is in the application context instead of the dispatcher context I am ...">

@RequestMapping annotation does not work if <context: component-scan / "> is in the application context instead of the dispatcher context

I am using Spring version 2.5.6 with <context: component-scan /> and @Autowired.

While I used SimpleUrlHandlerMapping in my dispatching context, everything was in order - auto installation worked fine, controllers correctly selected routes, 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 I decided to use @RequestMapping to configure my routes in an XML file.

@Controller("loginController")
public class LoginController {

    @RequestMapping("/login/login.html")
    public ModelAndView login() {
        ModelAndView model = new ModelAndView("login/login");
        return model;
    }

}

When I rewrote the code to use @RequestMapping , the problem started. The server (Tomcat) started giving me " No mapping for HTTP request with URI error .

, "< context: component-scan base-package =" com.example.springwebapp.controller "/ > " .

<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>

, , "< : - base-package =" com.example.springwebapp "/ > " 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"
    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>

, < context: component-scan/ > @RequestMapping. , , - , , , - ?

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>
+1
2

:

<mvc:annotation-driven />

Spring 2.5 , DefaultAnnotationHandlerMapping AnnotationMethodHandlerAdapter bean :

<context:annotation-config />
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
+1

( Jean-Philippe Bond), . , : - .

:

<context:component-scan base-package="com.example.springwebapp" use-default-filters="false">
        <context:include-filter expression="org.springframework.stereotype.Service" type="annotation"/>
        <context:include-filter expression="org.springframework.stereotype.Repository" type="annotation"/>
</context:component-scan>

:

<context:component-scan base-package="com.example.springwebapp" use-default-filters="false">
        <context:include-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>
0

Source: https://habr.com/ru/post/1607525/


All Articles