java - @RequestMapping annotation not working if <context:component-scan /> is in application context instead of dispatcher context -


i'm using spring of version 2.5.6 <context:component-scan /> , @autowired.

while i'd been using simpleurlhandlermapping in dispatcher context ok - autowiring working fine, routes picking controllers in right way etc.

<bean id="urlmapping" class="org.springframework.web.servlet.handler.simpleurlhandlermapping">     <property name="urlmap">         <map>             <entry key="/login/login.html" value-ref="logincontroller" />             <entry key="/secured/index/index.html" value-ref="indexcontroller" />         </map>     </property> </bean> 

then decided use @requestmapping insted of configuring routes in xml file.

@controller("logincontroller") public class logincontroller {      @requestmapping("/login/login.html")     public modelandview login() {         modelandview model = new modelandview("login/login");         return model;     }  } 

when had rewritten code use @requestmapping, problem started. server (tomcat) began give me "no mapping found http request uri" error.

i've found solution insert "<context:component-scan base-package="com.example.springwebapp.controller" />" element in dispatcher context config.

<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:aop="http://www.springframework.org/schema/aop"     xmlns:tx="http://www.springframework.org/schema/tx"     xsi:schemalocation="         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">      <!-- ============== -->     <!-- url mapping    -->     <!-- ============== -->  <!--    <bean id="urlmapping" class="org.springframework.web.servlet.handler.simpleurlhandlermapping"> --> <!--        <property name="urlmap"> --> <!--            <map> --> <!--                <entry key="/login/login.html" value-ref="logincontroller" /> --> <!--                <entry key="/secured/index/index.html" value-ref="indexcontroller" /> --> <!--            </map> --> <!--        </property> --> <!--    </bean> -->      <!-- ============ -->     <!-- viewresolver -->     <!-- ============ -->      <bean id="viewresolver" class="org.springframework.web.servlet.view.internalresourceviewresolver">         <property name="prefix" value="/web-inf/views/" />         <property name="suffix" value=".jsp" />     </bean>      <context:component-scan base-package="com.example.springwebapp.controller" />  </beans> 

i feel can bad solution because have similar "<context:component-scan base-package="com.example.springwebapp" />" in app context config file handle other dependencies in 1 shot.

<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"     xsi:schemalocation="         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">      <import resource="./spring-security.xml"/>     <import resource="../database/datasource.xml"/>     <import resource="../database/hibernate.xml"/>      <context:component-scan base-package="com.example.springwebapp" />  </beans> 

the question why <context:component-scan /> @ application level isn't picking @requestmapping annotations right? know can limit component scan base-package @ app level package, intention use 1 component-scan @ app level , nothing more. have use 2 distinct component-scan elements or maybe i'm missing something?

here web.xml:

<web-app xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"     xmlns="http://java.sun.com/xml/ns/javaee"     xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"     version="3.0">      <display-name>springwebapp</display-name>      <!-- ========== -->     <!-- spring mvc -->     <!-- ========== -->      <welcome-file-list>         <welcome-file>welcome.jsp</welcome-file>     </welcome-file-list>      <servlet>         <servlet-name>application</servlet-name>         <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class>         <init-param>             <param-name>contextconfiglocation</param-name>             <param-value>/web-inf/config/spring/mvc-context.xml</param-value>         </init-param>         <load-on-startup>1</load-on-startup>     </servlet>      <servlet-mapping>         <servlet-name>application</servlet-name>         <url-pattern>*.html</url-pattern>     </servlet-mapping>      <listener>         <listener-class>org.springframework.web.context.contextloaderlistener</listener-class>     </listener>      <context-param>         <param-name>contextconfiglocation</param-name>         <param-value>             /web-inf/config/spring/application-context.xml         </param-value>     </context-param>      <!-- =============== -->     <!-- spring security -->     <!-- =============== -->      <filter>         <filter-name>springsecurityfilterchain</filter-name>         <filter-class>org.springframework.web.filter.delegatingfilterproxy</filter-class>     </filter>      <filter-mapping>         <filter-name>springsecurityfilterchain</filter-name>         <url-pattern>/*</url-pattern>     </filter-mapping>  </web-app> 

try adding tag app context :

<mvc:annotation-driven /> 

edit

for spring 2.5 try adding annotation config tag, defaultannotationhandlermapping , annotationmethodhandleradapter bean app context :

<context:annotation-config /> <bean class="org.springframework.web.servlet.mvc.annotation.defaultannotationhandlermapping"/> <bean class="org.springframework.web.servlet.mvc.annotation.annotationmethodhandleradapter"/> 

Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

c# - Unity IoC Lifetime per HttpRequest for UserStore -

I am trying to solve the error message 'incompatible ranks 0 and 1 in assignment' in a fortran 95 program. -