2010-11-15 33 views
1

我在我的classpath中擁有Spring 3框架中的所有jar,並且我想將Spring 3 mvc添加到我的應用配置中。最初,我有以下的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: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,但後來我加了彈簧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" /> 

現在,我的問題都在我的應用程序。春天似乎不是以前的Autowiring bean。我還收到以下錯誤在我的控制器:

No adapter for handler [[email protected]]: Does your handler implement a supported interface like Controller? 
+1

是什麼意思「不能連接最多的資源」? – axtavt 2010-11-15 18:21:18

+0

對不起,我更新了該聲明。希望現在很清楚。 – stevebot 2010-11-15 18:27:57

+0

錯誤提到'LoginController'。所以告訴我們'LoginController'。 – skaffman 2010-11-15 18:40:55

回答

1

當您添加<mvc:annotation-driven />對上下文,你就有效地禁止了OLD-支持風格Controller類型層次結構。

錯誤消息告訴我,LoginController不是註釋控制器,而是Controller接口的子類型。

如果您不想重構LoginController,請從您的上下文中刪除<mvc:annotation-driven />。除非您使用JSR-303驗證或JSON序列化,否則您不需要它

2

我的工作snipplet對Spring MVC 3

<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"> 
+0

嗨gennad,你介意解釋這是做什麼完全通過編輯你的答案?這個bean頭文件如何提供幫助?謝謝! – jmort253 2012-08-02 00:54:18

3

除了skaffman的回答:您可以啓用舊式控制器而不移除<mvc:annotation-driven>,方法是聲明舊式處理程序映射和處理程序適配器手動:

<bean class = "org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" /> 
<bean class = "org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" /> 

說明:

DispatcherServlet找不到HandlerMapping S和在上下文中聲明HandlerAdapter S,它註冊的默認映射(BeanNameUrlHandlerMappingDefaultAnnotationHandlerMapping)和適配器(HttpRequestHandlerAdapter,SimpleControllerHandlerAdapterAnnotationMethodHandlerAdapter),所以在一個簡單的情況下,一切都沒有明確的配置。但是,如果明確聲明某些映射或適配器,則不應用缺省值,因此如果需要其他映射和適配器,則必須明確聲明它們。

現在,<mvc:annotation-driven>明確聲明DefaultAnnotationHandlerMappingAnnotationMethodHandlerAdapter,有效地禁用其他默認映射和適配器,因此您需要手動聲明它們。

1

通過axtavt的回答啓發,我從我的MVC配置除去這一點,這讓我過去「無適配器處理。」錯誤:

<bean id="annotationMapper" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">