我在我的类路径中拥有Spring 3框架中的所有jar,我想将Spring 3 mvc添加到我的app配置中。最初,我有以下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:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd
<context:annotation-config/>
<bean class="com.apppackage.app.config.AppContextConfig" />
<!-- Autoscan for @Controller type controllers -->
<context:component-scan base-package="com.apppackage.app.controller" />
这只是相关信息的一小部分。我的应用程序与上面的XML工作正常,但后来我在配置中添加了Spring 3 MVC,并进行了以下更改:
<?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:util="http://www.springframework.org/schema/util"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:annotation-config/>
<mvc:annotation-driven />
<bean class="com.apppackage.app.config.AppContextConfig" />
<!-- Autoscan for @Controller type controllers -->
<context:component-scan base-package="com.apppackage.app.controller" />
现在,我的应用程序遍布问题。 Spring似乎不是以前的Autowiring bean。我的控制器上也出现以下错误:
No adapter for handler [com.apppackage.app.controller.login.LoginController@274b9691]: Does your handler implement a supported interface like Controller?
答案 0 :(得分:3)
除了skaffman的回答:你可以通过手动声明旧式处理程序映射和处理程序适配器来启用旧式控制器而不删除<mvc:annotation-driven>
:
<bean class = "org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
<bean class = "org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
<强>解释强>
当DispatcherServlet
找不到上下文中声明的HandlerMapping
和HandlerAdapter
时,它会注册默认映射(BeanNameUrlHandlerMapping
和DefaultAnnotationHandlerMapping
)和适配器(HttpRequestHandlerAdapter
,SimpleControllerHandlerAdapter
和AnnotationMethodHandlerAdapter
),因此在一个简单的情况下,一切都可以在没有显式配置的情况下工作。但是,如果明确声明某些映射或适配器,则不应用默认值,因此如果需要其他映射和适配器,则必须明确声明它们。
现在,<mvc:annotation-driven>
明确声明DefaultAnnotationHandlerMapping
和AnnotationMethodHandlerAdapter
,有效地禁用其他默认映射和适配器,因此您需要手动声明它们。
答案 1 :(得分:2)
我为Spring 3 MVC工作的snipplet
<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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.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-3.0.xsd">
答案 2 :(得分:1)
在您的上下文中添加<mvc:annotation-driven />
时,您实际上是在禁用对旧式Controller
类型层次结构的支持。
错误消息告诉我LoginController
不是带注释的控制器,而是Controller
接口的子类型。
如果您不想重构LoginController
,请从您的上下文中删除<mvc:annotation-driven />
。除非您使用JSR-303验证或JSON序列化,否则无论如何都不需要它
答案 3 :(得分:1)
受到axtavt的回答的启发,我从我的mvc配置中删除了这个,这让我超越了“没有适配器处理程序..”错误:
<bean id="annotationMapper" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">