spring MVC 配置错误

在学习Spring MVC时,配置过程中遇到HTTP Status 404错误,环境为Spring 3.1.1。问题出现在尝试访问`http://localhost:8080/springMVC/welcome`时,服务器返回资源不可用的状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

   最近学习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


解决方法:
        将spring-servlet.xml中配置
      <mvc:annotation-driven/>
       <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>
    或者
      <mvc:annotation-driven/>
       <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子包下,所以可以查找到
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值