我的类路径上有 Spring 3 框架中的所有 jar,我想将 Spring 3 mvc 添加到我的应用程序配置中。最初,我有以下 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。我的 Controller 也出现以下错误:
No adapter for handler [com.apppackage.app.controller.login.LoginController@274b9691]: Does your handler implement a supported interface like Controller?
最佳答案
除了 skaffman 的回答:您可以在不删除 <mvc:annotation-driven>
的情况下启用旧式 Controller 通过手动声明旧式处理程序映射和处理程序适配器:
<bean class = "org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
<bean class = "org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
解释:
当 DispatcherServlet
找不到 HandlerMapping
s 和 HandlerAdapter
s 在上下文中声明,它注册默认映射( BeanNameUrlHandlerMapping
和 DefaultAnnotationHandlerMapping
)和适配器( HttpRequestHandlerAdapter
, SimpleControllerHandlerAdapter
和 AnnotationMethodHandlerAdapter
),所以在一个简单的情况下,没有显式配置一切正常。但是,如果您显式声明某些映射或适配器,则不会应用默认值,因此如果您需要其他映射和适配器,您也必须显式声明它们。
现在,<mvc:annotation-driven>
声明 DefaultAnnotationHandlerMapping
和 AnnotationMethodHandlerAdapter
显式、有效地禁用其他默认映射和适配器,因此您需要手动声明它们。
关于java - 使用 Spring 3 MVC 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4187503/