springmvc添加<mvc:default-servlet-handler/>后controller失效,报404
问题springmvc.xml代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.baidu"/>
<mvc:default-servlet-handler/>
</beans>
原因: 使用springMVC的<mvc:default-servlet-handler/>处理静态资源访问时,静态资源会交给Tomcat容器内部的DefaultServletHttpRequestHandler
进行处理,导致注解处理器RequestMappingHandlerMapping
和注解适配器RequestMappingHandlerAdapter
失效。
解决方法:
方法一:在mvc:default-servlet-handler
前面添加mvc:annotation-driven
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.baidu"/>
<!--解决方法-->
<mvc:annotation-driven />
<mvc:default-servlet-handler/>
</beans>
方法二:在mvc:default-servlet-handler
前面添加注释处理映射器
和注解处理适配器
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.baidu"/>
<!--配置注释处理映射器-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
<!--配置注解处理适配器-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
<mvc:default-servlet-handler/>
</beans>
注解解释:
<mvc:default-servlet-handler/>:
静态资源交给Tomcat容器自身处理,内部采用DefaultServletHttpRequestHandler处理。优先级最低,且可以匹配所有的请求:“/**”。如果其他所有的handler都匹配不上,请求会转向"/**",这个handler就是处理静态资源的处理方式:将会转到tomcat中的DefaultServletHttpRequestHandler处理。
<mvc:annotation-driven/>:
注册默认处理请求、参数和返回值的类,主要由两个类RequestMappingHandlerMapping和RequestMappingHandlerAdapter。RequestMappingHandlerMapping是处理@RequestMapper注解,并将其注册到请求映射表中;RequestMappingHandlerAdapter是处理请求的适配器,确定调用哪个类的哪个方法,并且构造方法的参数,返回值。当配置了<mvc:annotation-driven/>后,spring就知道我们启用了注解驱动,然后spring通过<context:component-scan/>标签配置,自动扫描@Component,@Controller,@Service,@Repository等注解标记的组件注册到工厂中,来处理我们的请求。