学无先后,达者为师

网站首页 编程语言 正文

SpringMVC——访问action报404错误详解

作者:xinlianluohan 更新时间: 2023-12-13 编程语言

1、检查URL是否会被springmvc拦截

首先检查访问路径URL是否正确被springmvc拦截,这个URL和web.,xml中对springmvc的配置有关。

2、检查Controller层是否使用@Controller注解

检查是否在Controll层使用@Controller注解来将其标注为组件;

只有使用了@Controller标注的Controller层才能被Springmvc识别.

3、检查是否配置了<context:component-scan/><mvc:annotation-driven/>(springmvc.xml中)

3.1、检查是否配置了<context:component-scan/>
        1、<context:component-scan/>是用来指明让springmvc容器去哪里扫描@Controller注解,只有springmvc找到了@Controller注解,springmvc才能进入Controller.

        2、配置方式为:<context:component-scan base-package="com.taotao.controller" /> 

3.2、检查<context:component-scan/>是否配置正确
        1、Base-package后面如果写成”com.taotao.controller.*”是错误的(不带*,定位到包即可).

        2、Base-package后面的路径指的是Controller层所在的包,如图,注意路径一定要写对.

3.3、检查是否配置了<mvc:annotation-driven/>
<mvc:annotation-driven/>相当于注册了DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter两个bean,配置一些messageconverter。即解决了@Controller注解的使用前提配置。配置方式为:<mvc:annotation-driven />

ps:<mvc:annotation-driven/>和<context:component-scan/>不分先后顺序

4、检查是否正确读取Springmvc的配置文件(web.xml中)

4.1、如果没有配置<init-param></init-param>
        那么spring容器会自动去WEB-INF目录下扫描一个叫SpringMVC-servlet.xml的配置文件来加载springmvc容器.

4.2、如果如下图配置了<init-param></init-param>
        重点检查一下是否classpath后面的路径书写的有错误.

注意:

        1.只有成功读取到springmvc的xml配置文件,才能让spring容器加载springmvc容器,然后才能识别@Controller注解并执行标注的Contoller层

        2.<param-name></param-name>之间的contextConfigLocation是固定的


PS:

web.xml加载spring容器的xml配置文件时,<param-name></param-name>之间的contextConfigLocation也是固定的.

5、检查是否成功加载spring容器

如果spring容器都未成功加载,那么肯定无法成功加载springmvc容器。

5.1、检查spring容器的监听是否正常启动

<listener>   
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
</listener>  


5.2、检查是否正确读取了spring容器的xml配置文件
  

注意:再次强调,<param-name>标签中的contextConfigLocation是固定的,书写错误会报错

原文链接:https://gaodengwen.blog.csdn.net/article/details/132713697

  • 上一篇:没有了
  • 下一篇:没有了
栏目分类
最近更新