Spring 4 + Apache Tiles 3显示纯文本

时间:2014-03-31 15:24:31

标签: spring jsp spring-mvc tiles apache-tiles

我将使用Spring 4和Apache Tiles 3设置一个新项目。我有一个奇怪的问题。

当加载我的tile定义时,控制器将显示所请求的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>

Spring登录请求

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.springframework.web.servlet.resource.DefaultServletHttpRequestHandler@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 个答案:

答案 0 :(得分:3)

我在 web.xml 中使用/ *而不是/

映射应用程序servlet时出了大错
<servlet-mapping>
    <servlet-name>applicationServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

答案 1 :(得分:2)

您需要在view.xml文件中包含tilesConfigurer bean:

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

答案 2 :(得分:1)

尝试配置视图解析器bean,如:

<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>

tiles-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>

然后,从您的Controller返回切片定义名称。

像:

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