zoukankan      html  css  js  c++  java
  • SpringMvc配置扫包之后,访问路径404问题解决

    场景:

      1. 配置了Spring和SpringMvc, Spring管理非Controller类的Bean, SpringMvc管理涉及的Controller类

      2. web.xml已经配置了Spring的监听器, SpringMvc的servlet映射

      3. 包扫描配置正确

    问题:

      访问指定的API链接,发现返回404, 日志显示:"No mapping found for HTTP request with URI [/xxx.do] in DispatcherServlet with name 'xxx' ”

    排查后发现:

      SpringMvc除了org.springframework.web.servlet.DispatcherServlet之外,还需要配置两个类:

    (Spring 3.1之前的做法)

    <!--配置注解控制器映射器,它是SpringMVC中用来将Request请求URL到映射到具体Controller-->
    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
    
    <!--配置注解控制器映射器,它是SpringMVC中用来将具体请求映射到具体方法-->
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/> 

    (Spring3.1之后的做法)

    <!--配置注解控制器映射器,它是SpringMVC中用来将Request请求URL到映射到具体Controller--> 
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> 
    <!--配置注解控制器映射器,它是SpringMVC中用来将具体请求映射到具体方法--> 
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>

    还有一种简便做法是在SpringMvc的配置文件中加入:<mvc:annotation-driven />即可代替上面两行配置

    参考文章: SpringMVC和Spring的配置文件扫描包详解 https://blog.csdn.net/uniqueweimeijun/article/details/72636481

  • 相关阅读:
    浅谈ssh(struts,spring,hibernate三大框架)整合的意义及其精髓
    Spring中ClassPathXmlApplicationContext类的简单使用
    测试计划/系统风险 (设计方面、开发方面、测试本身 等风险)
    浏览器兼容
    4.0 爬虫
    2.1 IDEA
    2.3 接口测试
    1.1测试分类
    1.3 ODPS
    2.1 Word 插入 smartart、图表
  • 原文地址:https://www.cnblogs.com/zhuangmingnan/p/9791109.html
Copyright © 2011-2022 走看看