最近学习spring MVC, 发现不懂原理直接按照网上的小例子配置, 遇到错误真的崩溃
环境: spring3..1.1
src 目录
com.mvc.rest
RestController.java
错误:
http://localhost:8080/springMVC/welcome
显示:
HTTP Status 404 -
type Status report
message
description The requested resource is not available.
Apache Tomcat/8.0.9
日志:
2016-04-08 15:40:22 Did not find handler method for [/welcome]
2016-04-08 15:40:22 No mapping found for HTTP request with URI [/springMVC/welcome] in DispatcherServlet with name 'spring'
2016-04-08 15:40:22 Successfully completed request
2016-04-08 15:40:22 No mapping found for HTTP request with URI [/springMVC/welcome] in DispatcherServlet with name 'spring'
2016-04-08 15:40:22 Successfully completed request
解决方法:
将spring-servlet.xml中配置
<mvc:annotation-driven/>
<context:component-scan base-package="com.mvc.rest/*"></context:component-scan>
<context:component-scan base-package="com.mvc.rest/*"></context:component-scan>
改为
<mvc:annotation-driven/>
<context:component-scan base-package="com.mvc.rest"></context:component-scan>
<context:component-scan base-package="com.mvc.rest"></context:component-scan>
或者
<mvc:annotation-driven/>
<context:component-scan base-package="com.mvc.**"></context:component-scan>
<context:component-scan base-package="com.mvc.**"></context:component-scan>
因为 base-package 扫描的是包路径, 如果配置com.mvc.rest.* 扫描的是com.mvc.rest下的子包路,因为com.mvc.rest下没有子包路径
如果配置的com.mvc.* 则扫描com.mvc下所有的子包路径,因为RestController 在rest子包下,所以可以查找到