从子文件夹内部调用时,不会加载Tiles 2

时间:2013-06-17 19:10:26

标签: web-applications servlets spring-mvc tiles2

我的webapp存在这些奇怪的问题。当我访问 home.jsp example.jsp 时,正常加载了Tiles,但是当我想要访问时 example2.jsp ,在子文件夹中,根本不会调用布局。

我的目录结构如下:

webapp
  |___ pages
           |___ subfolder
                      |___ example2.jsp
           |___ tiles
                  |___ layouts
                         |___ base-layout.jsp
                  |___ header.jsp
                  |___ footer.jsp
           |___ example.jsp
           |___ home.jsp
  |___ resources
  |___ WEB-INF
           |___ servlet.xml
           |___ tiles-defs.xml
           |___ web.xml
  |___ index.jsp

我的 servlet.xml

<!-- JSP View Resolver -->  
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/pages/" />
    <property name="suffix" value=".jsp" /> 
    <property name="order" value="1" />
</bean>

<!-- Default Tiles View Resolver -->    
<bean id="tilesViewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver" >
    <property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView"/> 
    <property name="order" value="0" />
</bean> 

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

我的 tiles-defs.xml

<tiles-definitions>

  <definition name="base-layout" template="/pages/tiles/layouts/base-layout.jsp">
    <put-attribute name="header" value="/pages/tiles/header.jsp" />
    <put-attribute name="body" value="" />
    <put-attribute name="footer" value="/pages/tiles/footer.jsp" />
  </definition>

  <definition name="home" extends="base-layout">
    <put-attribute name="title" value="Title" /> 
    <put-attribute name="body" value="/pages/home.jsp" />
  </definition>

  <definition name="example1" extends="base-layout">
    <put-attribute name="title" value="Title" /> 
    <put-attribute name="body" value="/pages/example.jsp" />
  </definition>

  <definition name="example2" extends="base-layout">
    <put-attribute name="title" value="Title" /> 
    <put-attribute name="body" value="/pages/subfolder/example2.jsp" />
  </definition>

</tiles-definitions>

我的 web.xml

<servlet>
        <servlet-name>myServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>myServlet</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>

2 个答案:

答案 0 :(得分:1)

最后,我通过评论 InternalResourceViewResolver 中的 viewClass 行来实现它。

最终代码如下:

<!-- JSP View Resolver -->  
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> -->
<property name="prefix" value="/pages/" />
<property name="suffix" value=".jsp" /> 
<property name="order" value="1" />

答案 1 :(得分:0)

  

当我访问home.jsp或example.jsp时,[snip]但是当我想访问example2.jsp [snip]

您对这些请求使用的网址是什么?

您的spring DispatcherServlet是针对与* .html网址匹配的请求配置的。所有其他网址都不会初始化图块。