0

i use Spring MVC with Spring 3.1

I place all the base-packages in the applicationContext.xml for the automatic component scanning. However, when im trying this, im getting the error:

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

I resolved the error above by adding the controller package in the test-servlet.xml

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

Why do i have to add explicitly the controller package in the test-servlet.xml, when i already add all my packages in the 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" />

test-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>
user711189
  • 4,383
  • 4
  • 30
  • 48

1 Answers1

0

By default controllers should be declared in test-servlet.xml, not in applicationContext.xml.

In your case you can move <context:component-scan> for controller package to test-servlet.xml.

If package with controllers also contains other beans, you can use an approach described here. Also note that your package layout is considered to be an antipattern, it's recommended to package by feature rather than by layer.

Alternatively, you can override default behaviour using detectHandlersInAncestorContexts property.

Community
  • 1
  • 1
axtavt
  • 239,438
  • 41
  • 511
  • 482