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容器的监听是否正常启动
5.2、检查是否正确读取了spring容器的xml配置文件
注意:再次强调,<param-name>标签中的contextConfigLocation是固定的,书写错误会报错
希望可以帮到大家!!!