springmvc添加<mvc:default-servlet-handler/>后controller失效,报404

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等注解标记的组件注册到工厂中,来处理我们的请求。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值