Spring webmvc链接viewresolvers给出了BeanNotOfRequiredTypeException

时间:2012-06-16 15:20:02

标签: java spring

我有一个简单的(示例)Web应用程序,如果我尝试链接多个viewresolvers我有问题。当我的应用程序以下面描述的方式配置时,尝试访问http://localhost:8080/Library/spring/initApplication将引发下面描述的错误。

似乎错误的视图解析器错误地选择了InitApplication。如果我改变视图解析器的顺序一切正常,但我仍然想知道为什么会发生这个错误,因为我无法确定它为什么会发生。我猜一个配置问题?

26027 ["http-bio-8080"-exec-6] DEBUG org.springframework.web.servlet.DispatcherServlet  - Could not complete request
org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'initApplication' must be of type [org.springframework.web.servlet.View], but was actually of type [library.controller.InitApplication]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:349)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:266)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1079)
    at org.springframework.web.servlet.view.ResourceBundleViewResolver.loadView(ResourceBundleViewResolver.java:196)
    at org.springframework.web.servlet.view.AbstractCachingViewResolver.createView(AbstractCachingViewResolver.java:158)
    at org.springframework.web.servlet.view.AbstractCachingViewResolver.resolveViewName(AbstractCachingViewResolver.java:77)
    at org.springframework.web.servlet.DispatcherServlet.resolveViewName(DispatcherServlet.java:1078)

重要文件的详细信息: InitApplication.java

@Controller
public class InitApplication {


    @RequestMapping(value = "/initApplication", method = RequestMethod.GET)
etc.......

的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>testwebproj</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/spring/*</url-pattern>
    </servlet-mapping>
</web-app>

用SpringMVC-servlet.xml中

<?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:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"

    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/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

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

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost/library" />
        <property name="username" value="root" />
        <!-- <property name="password" value="" /> -->
    </bean>

    <bean id="myEmf"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="jpaDialect" ref="jpaDialect" />
    </bean>

    <bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="myEmf" />
    </bean>


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


<tx:annotation-driven transaction-manager="transactionManager" />
<context:component-scan base-package="library"></context:component-scan>
</beans>

views.properties

pdfPrinter.(class)=library.controller.DummyController

2 个答案:

答案 0 :(得分:1)

肯定是因为您正在使用Controller类配对映射您的视图!

pdfPrinter.(class)= you should indicate a View class here!

例如,要将Controller映射到jsp View,您需要按以下步骤操作:

login.(class)=org.springframework.web.servlet.view.JstlView
login.url=/vues/en/login.jsp

答案 1 :(得分:0)

你试过重命名你的bean吗?你可能正在破坏spring使用的内部bean