无法使用tiles3 spring mvc 4显示模板视图

时间:2014-03-18 18:10:16

标签: spring tiles-3

我使用tiles3来显示模板布局。但我得到一个例外。我尝试将视图解析器更改为URLbasedviewresolver。但错误是一样的。如果我使用intenalviewresolver我能够获得默认的index.jsp页面。但它没有显示模板定义。请帮忙。

这是我的调度程序服务器代码

    <beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

    <context:component-scan base-package="registration" />

    <mvc:annotation-driven />

    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basenames" value="classpath:messages"/>
    <property name="defaultEncoding" value="UTF-8"/>
    </bean>

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

以下是我的tiles-defs.xml文件内容

    <?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE tiles-definitions PUBLIC
       "-//Apache Software Foundation//DTD Tiles Configuration 3.0//EN"
       "http://tiles.apache.org/dtds/tiles-config_3_0.dtd">

<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="index" extends="baseLayout">
      <put-attribute name="body"   value="/WEB-INF/jsp/index.jsp"/>
  </definition>

</tiles-definitions>

这是控制器类中的代码

    @RequestMapping("/index.htm")
   public String index()
    {
            return "index";
    }

以下是我得到的例外

javax.servlet.ServletException: Could not resolve view with name 'index' in servlet with name 'dispatcher'
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1208)
org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1012)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)

通过定义视图属性文件来尝试使用ResourceBundleViewResolver。这是更改的调度程序servlet代码

<bean id="viewResolver1" class="org.springframework.web.servlet.view.ResourceBundleViewResolver" 
    p:basename="views" p:order="1"/>

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
      p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" p:order="3" />

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

这是视图属性文件条目

index.(class)=org.springframework.web.servlet.view.tiles3.TilesView
index.url=index

现在以下是我得到的例外

org.apache.tiles.definition.NoSuchDefinitionException: Unable to find the definition 'index'
org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:218)
org.apache.tiles.renderer.DefinitionRenderer.render(DefinitionRenderer.java:59)
org.springframework.web.servlet.view.tiles3.TilesView.renderMergedOutputModel(TilesView.java:114)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:266)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1225)
org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1012)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)

1 个答案:

答案 0 :(得分:0)

我已经删除并重新创建了该项目。这次修改了调度程序servlet的内容。它现在正在运作。我能够显示瓷砖模板。我认为我在提前给出名称空间和架构位置时犯了一些错误。我在这里附加修改后的调度程序servlet代码。

 <?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?xml version="1.0" encoding="UTF-8"?> -->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">


    <mvc:annotation-driven />

    <context:component-scan base-package="registration" />


    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basenames" value="classpath:messages"/>
    <property name="defaultEncoding" value="UTF-8"/>
    </bean>

    <bean id="viewResolver1" class="org.springframework.web.servlet.view.ResourceBundleViewResolver" 
        p:basename="views" />

    <bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer" >
         <property name="definitions">
             <value>/WEB-INF/tilesdefs.xml</value>
         </property>
    </bean>
    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp" />
</beans>