Spring Tiles应用程序:在ServletContext资源中定义名称为viewResolver的bean创建时出错

时间:2015-04-28 12:44:11

标签: java spring spring-mvc servlets tiles

我正在尝试使用Spring MVC创建一个简单的tile应用程序,但是当我启动服务器时,我得到了以下错误,我无法弄明白。

错误

org.springframework.beans.factory.BeanCreationException:创建名称为' viewResolver'的bean时出错在ServletContext资源[/WEB-INF/dispatcher-servlet.xml]中定义:设置属性值时出错;嵌套异常是org.springframework.beans.PropertyBatchUpdateException;嵌套的PropertyAccessExceptions(1)是: PropertyAccessException 1:org.springframework.beans.MethodInvocationException:Property' viewClass'抛出异常;嵌套异常是java.lang.IllegalArgumentException:给定视图类[org.springframework.web.servlet.view.tiles2.TilesView]不是[org.springframework.web.servlet.view.InternalResourceView]类型

以下是我的文件

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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>SpringMVC</display-name>

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

   <servlet-mapping>
      <servlet-name>dispatcher</servlet-name>
      <url-pattern>/</url-pattern>
   </servlet-mapping>

    <context-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>  
    </context-param>  

</web-app>

servlet.xml中

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans     
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-package="com.tutorialpoint" />
<mvc:annotation-driven />

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

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

</beans>

tiles.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="base.definition"  
        template="/WEB-INF/jsp/layout.jsp">  
        <put-attribute name="title" value="" />  
        <put-attribute name="header" value="/WEB-INF/jsp/header.jsp" />  
        <put-attribute name="menu" value="/WEB-INF/jsp/menu.jsp" />  
        <put-attribute name="body" value="" />  
        <put-attribute name="footer" value="/WEB-INF/jsp/footer.jsp" />  
    </definition>  

    <definition name="contact" extends="base.definition">  
        <put-attribute name="title" value="Contact Manager" />  
        <put-attribute name="body" value="/WEB-INF/jsp/contact.jsp" />  
    </definition>  

    <definition name="hello" extends="base.definition">  
        <put-attribute name="title" value="Hello Spring MVC" />  
        <put-attribute name="body" value="/WEB-INF/jsp/hello.jsp" />  
    </definition>  

</tiles-definitions>  

请指导。

1 个答案:

答案 0 :(得分:1)

使用的视图解析程序不兼容,请尝试使用UrlBasedViewResolver

示例配置:

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

示例应用here