2011-12-28 23 views
0

發現映射我使用Spring MVC與Spring 3.1彈簧MVC中,沒有在DispatcherServlet的,基本包註解

我放置在applicationContext.xml所有基包自動組件掃描。然而,當我嘗試這一點,即時得到錯誤:

No mapping found for HTTP request with URI [/Test1/] in DispatcherServlet with name 'test' 

我通過在測試servlet.xml中添加控制器封裝解決上述錯誤

<context:component-scan base-package="com.george.controller" /> 

爲什麼我一定要明確添加test-servlet.xml中的控制器包,當我已經將所有包添加到applicationContext.xml中時?

的web.xml

<context-param> 
<param-name>contextConfigLocation</param-name> 
<param-value> 
     /WEB-INF/spring/applicationContext.xml 
     /WEB-INF/spring/dataContext.xml 
</param-value> 
</context-param> 

<listener> 
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 

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

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

的applicationContext.xml

<context:annotation-config /> 

<context:component-scan base-package="com.george.dao, com.george.service, com.george.controller" /> 

<aop:aspectj-autoproxy /> 
<tx:annotation-driven transaction-manager="transactionManager" /> 

測試servlet.xml中

<mvc:annotation-driven /> 


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

回答

0

通過德發應該在test-servlet.xml中聲明ult控制器,而不是在applicationContext.xml中聲明。

在你的情況下,你可以移動<context:component-scan>controller包到test-servlet.xml

如果包含控制器的包還包含其他bean,則可以使用描述爲here的方法。另請注意,您的包佈局被認爲是反模式,建議按功能而不是按層進行打包。

或者,您可以覆蓋默認行爲using detectHandlersInAncestorContexts property

相關問題