Spring MVC的Tiles 2无法解析

时间:2013-03-28 15:45:03

标签: spring-mvc tiles2

我正在尝试将Tiles 2与Spring MVC集成,但我得到的是一个特定的jsp而不是Tiles View。例如,如果request =“about.htm”,那么我将获得about.jsp,它保存在/jsp/about.jsp中。它的工作方式就像通过UrlBasedViewResolver解析视图一样。 我的Spring-servlet.xml是

<mvc:annotation-driven />
    <context:component-scan base-package="com.spring.controller" />

    <mvc:resources mapping="/resources*" location="resources/"/>



    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/"/>
    <property name="suffix" value=".jsp"/>
    </bean>

    <bean id="TilesViewResolver" class="org.springframework.web.servlet.view.ResourceBundleViewResolver" p:basename="views" />
    <bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer" p:definitions="/WEB-INF/tiles-defs.xml" />

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
        <constructor-arg ref="dataSource"/>
    </bean>

    <bean id="loginDao" class="com.spring.dao.impl.LoginDaoImpl">
        <property name="jdbcTemplate" ref="jdbcTemplate"/>
    </bean>

保留在类下的views.properties是

welcome.(class)=org.springframework.web.servlet.view.tiles2.TilesView
welcome.url=welcome

friends.(class)=org.springframework.web.servlet.view.tiles2.TilesView
friends.url=friends

office.(class)=org.springframework.web.servlet.view.tiles2.TilesView
office.url=office

about.(class)=org.springframework.web.servlet.view.JstlView
about.url=/jsp/about.jsp

tiles-defs.xml是

<tiles-definitions>

<definition name="baseLayout" template="/WEB-INF/tiles/baseLayout.jsp">
  <put-attribute name="title"  value="Template"/>
  <put-attribute name="header" value="/WEB-INF/tiles/header.jsp"/>
  <put-attribute name="menu"   value="/WEB-INF/tiles/menu.jsp"/>
  <put-attribute name="body"   value="/WEB-INF/tiles/body.jsp"/>
  <put-attribute name="footer"   value="/WEB-INF/tiles/footer.jsp"/>
</definition>

<definition name="welcome" extends="baseLayout">
  <put-attribute name="title"  value="Welcome"/>
  <put-attribute name="body"   value="/jsp/welcome.jsp"/>      
</definition>

<definition name="friends" extends="baseLayout">
  <put-attribute name="title"  value="Friends"/>
  <put-attribute name="body"   value="/jsp/friends.jsp"/>      
</definition>

<definition name="office" extends="baseLayout">
  <put-attribute name="title"  value="Office"/>
  <put-attribute name="body"   value="/jsp/office.jsp"/>      
</definition>

</tiles-definitions>

请指导。

1 个答案:

答案 0 :(得分:1)

视图解析器顺序可能有问题,如果要优先使用切片视图解析器,则为视图解析器添加顺序属性

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/"/>
    <property name="suffix" value=".jsp"/>
    <property name="order" value="2"/>
</bean>
<bean id="TilesViewResolver" class="org.springframework.web.servlet.view.ResourceBundleViewResolver" p:basename="views" p:order="1"/>