2014-03-31 138 views
0

我將用Spring 4和Apache Tiles 3設置一個新項目。我有一個奇怪的問題。Spring 4 + Apache Tiles 3顯示純文本

當我的瓷磚定義被加載,而不是顯示請求的JSP,控制器返回到瀏覽器的純文本沒有呈現。

view.xml用

<bean id="tilesViewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver"> 
    <property name="viewClass" value="org.springframework.web.servlet.view.tiles3.TilesView"/> 
</bean> 

<bean class="org.springframework.web.servlet.view.tiles3.TilesConfigurer" id="tilesConfigurer"> 
    <property name="definitions"> 
     <list> 
      <value>/WEB-INF/tiles/tiles-definitions.xml</value> 
     </list> 
    </property> 
</bean> 

磚,definition.xml

<tiles-definitions> 
    <definition name="welcome" template="/WEB-INF/view/jsp/template.jsp" > 
     <put-attribute name="header" value="Test" /> 
     <put-attribute name="body" value="Test" /> 
     <put-attribute name="footer" value="Test" /> 
    </definition> 

template.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> 
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %> 
<!doctype html> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title>Insert title here</title> 
    </head> 
    <body> 
     <div id="header"><tiles:insertAttribute name="header"/></div> 
     <div id="body"><tiles:insertAttribute name="body"/></div> 
     <div id="footer"><tiles:insertAttribute name="footer"/></div> 
    </body> 
</html> 

春天登錄請求

17:23:55.637 [http-bio-8080-exec-8] DEBUG o.s.web.servlet.DispatcherServlet - DispatcherServlet with name 'applicationServlet' processing GET request for [/eurostart/] 
17:23:55.637 [http-bio-8080-exec-8] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Looking up handler method for path/
17:23:55.637 [http-bio-8080-exec-8] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Returning handler method [public java.lang.String it.insiel.eurostart.controller.WelcomeController.home()] 
17:23:55.638 [http-bio-8080-exec-8] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'welcomeController' 
17:23:55.638 [http-bio-8080-exec-8] DEBUG o.s.web.servlet.DispatcherServlet - Last-Modified value for [/eurostart/] is: -1 
17:23:55.639 [http-bio-8080-exec-8] DEBUG o.s.web.servlet.DispatcherServlet - Rendering view [org.springframework.web.servlet.view.tiles3.TilesView: name 'welcome'; URL [welcome]] in DispatcherServlet with name 'applicationServlet' 
17:23:55.639 [http-bio-8080-exec-8] DEBUG o.a.tiles.impl.BasicTilesContainer - Render request received for definition 'welcome' 
17:23:55.640 [http-bio-8080-exec-8] DEBUG o.s.web.servlet.DispatcherServlet - DispatcherServlet with name 'applicationServlet' processing GET request for [/eurostart/WEB-INF/view/jsp/template.jsp] 
17:23:55.640 [http-bio-8080-exec-8] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Looking up handler method for path /WEB-INF/view/jsp/template.jsp 
17:23:55.640 [http-bio-8080-exec-8] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Did not find handler method for [/WEB-INF/view/jsp/template.jsp] 
17:23:55.641 [http-bio-8080-exec-8] DEBUG o.s.w.s.h.SimpleUrlHandlerMapping - Matching patterns for request [/WEB-INF/view/jsp/template.jsp] are [/**] 
17:23:55.641 [http-bio-8080-exec-8] DEBUG o.s.w.s.h.SimpleUrlHandlerMapping - URI Template variables for request [/WEB-INF/view/jsp/template.jsp] are {} 
17:23:55.641 [http-bio-8080-exec-8] DEBUG o.s.w.s.h.SimpleUrlHandlerMapping - Mapping [/WEB-INF/view/jsp/template.jsp] to HandlerExecutionChain with handler [org.spring[email protected]5f37e070] and 1 interceptor 
17:23:55.641 [http-bio-8080-exec-8] DEBUG o.s.web.servlet.DispatcherServlet - Last-Modified value for [/eurostart/WEB-INF/view/jsp/template.jsp] is: -1 
17:23:55.642 [http-bio-8080-exec-8] DEBUG o.s.web.servlet.DispatcherServlet - Null ModelAndView returned to DispatcherServlet with name 'applicationServlet': assuming HandlerAdapter completed request handling 
17:23:55.642 [http-bio-8080-exec-8] DEBUG o.s.web.servlet.DispatcherServlet - Successfully completed request 
17:23:55.643 [http-bio-8080-exec-8] DEBUG o.s.web.servlet.DispatcherServlet - Successfully completed request 

感謝所有誰幫我。

回答

3

我做了一個大錯web.xml中映射與應用程序的servlet/*代替/

<servlet-mapping> 
    <servlet-name>applicationServlet</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping> 
+0

如果這是答案,那麼你應該標記它。 – Rembo

2

您需要在您的view.xml用文件的TilesConfigurer豆:

<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer" > 
     <property name="definitions"> 
      <value>/WEB-INF/tiles/tiles-definitions.xml</value> 
     </property> 
    </bean> 
+0

THX,尋找你的答案小時後幫了我。 – Chris

1

嘗試配置視圖解析器豆狀:

<bean id="viewResolver" 
     class="org.springframework.web.servlet.view.UrlBasedViewResolver"> 
    <property name="viewClass"> 
      <value> 
       org.springframework.web.servlet.view.tiles3.TilesView 
      </value> 
    </property> 
</bean> 
    <!-- The actual tiles templates are in the tiles-definitions.xml -->   
<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer"> 
    <property name="definitions">  
     <list> 
      <value>/WEB-INF/tiles/tiles-definitions.xml</value> 
     </list>  
    </property> 
</bean> 

個瓷磚-definition.xml

<tiles-definitions> 
    <definition name="base.definition" 
      template="/WEB-INF/view/jsp/layout.jsp"> 
     <put-attribute name="title" value="" /> 
     <put-attribute name="header" value="/WEB-INF/view/jsp/template/header.jsp" /> 

     <put-attribute name="body" value="" /> 
     <put-attribute name="footer" value="/WEB-INF/view/jsp/template/footer.jsp" /> 
    </definition> 
    <definition name="welcome" extends="base.definition"> 
     <put-attribute name="title" value="Home" /> 
     <put-attribute name="body" value="/WEB-INF/view/jsp/welcome.jsp" /> 
    </definition> 
</tiles-definitions> 

然後,從控制器返回瓷磚定義名稱。

像:

@RequestMapping(method=RequestMethod.GET) 
public String welcome(){ 
    return "welcome"; //tiles definition name 
} 
相關問題