2

我是 Spring 3 和 Tiles 2.2.2 的新手。我正在关注http://static.springsource.org/spring/docs/3.0.x/reference/view.html上的 Spring 文档来配置 Tiles,但模板没有显示在我的测试页面上。

这是我的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>livros</display-name>
  <welcome-file-list>
    <welcome-file>formulario-login.jsp</welcome-file>  
  </welcome-file-list>

  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>    
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>


  <filter>
     <filter-name>SetCharacterEncodingFilter</filter-name>
     <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
     <init-param>
         <param-name>encoding</param-name>
         <param-value>UTF8</param-value>
     </init-param>
     <init-param>
         <param-name>forceEncoding</param-name>
         <param-value>true</param-value>
     </init-param>
  </filter>
  <filter-mapping>
     <filter-name>SetCharacterEncodingFilter</filter-name>
     <url-pattern>/*</url-pattern>
  </filter-mapping>

</web-app>

...这是我的spring-context.xml

    <mvc:annotation-driven />

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

    <mvc:default-servlet-handler/>

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

    <bean id="viewResolver" class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
      <property name="basename" value="views"/>
    </bean>

    <mvc:interceptors>
        <bean class="br.com.coldsoft.livros.interceptor.AutorizadorInterceptor" />
    </mvc:interceptors>


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

瓷砖-defs.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE tiles-definitions PUBLIC
       "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
       "http://tiles.apache.org/dtds/tiles-config_2_0.dtd">

<tiles-definitions>
    <definition name="layout"
        template="/WEB-INF/templates/layout.jsp">
        <put-attribute name="title" value="Controle de Livros" />
        <put-attribute name="menu" value="/WEB-INF/templates/menu.jsp" />
        <put-attribute name="body" value="/WEB-INF/templates/body.jsp" />
        <put-attribute name="footer" value="/WEB-INF/templates/footer.jsp" />
    </definition>

    <definition name="main" extends="layout">
        <put-attribute name="title" value="Main"></put-attribute>
        <put-attribute name="body" value="/WEB-INF/views/main.jsp"></put-attribute>
    </definition>    
</tiles-definitions>

布局.jsp

<html>
<head>
    <!-- Titulo Controle de Livros -->
    <title><tiles:insertAttribute name="title" ignore="true" /></title>
</head>

<body>
    <tiles:insertAttribute name="menu" />
    <tiles:insertAttribute name="body" />
    <tiles:insertAttribute name="footer" />
</body>
</html>

视图.属性

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

当我访问http://myserver:8080/livros/main它时,它只显示这个 main.jsp 的内容

<div class="container">
    <h3>Bem vindo, ${usuarioLogado.login}!!!!!!!!!!!!!!!!</h3>
</div>

如何显示我在 layout.jsp 上创建的模板(它们位于 WEB-INF/templates)?

请帮帮我!!

4

1 回答 1

2

替换为viewResolver

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

并删除InternalResourceViewResolver

于 2013-02-20T07:42:51.777 回答