如果@RequestMapping注释不起作用 在应用程序上下文中而不是调度程序上下文中
问题内容:
我正在使用带有
当我在调度程序上下文中使用 SimpleUrlHandlerMapping时 ,一切都很好-自动装配工作正常,路由以正确的方式选择控制器等。
<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>
然后,我决定使用 @RequestMapping 来在XML文件中配置路由。
@Controller("loginController")
public class LoginController {
@RequestMapping("/login/login.html")
public ModelAndView login() {
ModelAndView model = new ModelAndView("login/login");
return model;
}
}
当我重写代码以使用 @RequestMapping时 ,问题开始了。服务器(Tomcat)开始给我一个“
找不到带有URI的HTTP请求的映射 ”错误。
我发现解决方案是在调度程序上下文配置中插入一个 “
<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>
我觉得这可能是一个不好的解决方案,因为我的应用上下文配置文件中有类似的 “
<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>
问题答案:
尝试将此标签添加到您的应用上下文中:
<mvc:annotation-driven />
编辑
要Spring 2.5
尝试通过在您的应用上下文中添加注释config标签,a
DefaultAnnotationHandlerMapping
和AnnotationMethodHandlerAdapter
Bean:
<context:annotation-config />
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>