问题可能出现在web.xml中的url-pattern,在web项目配置SpringMVC提供的DispatcherServlet时,拦截路径写错了。
以下是正确的代码截图:

以下是正确的代码:
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/example/*</url-pattern>
</servlet-mapping>
错误的代码1:
<url-pattern>/date0906_war_exploded/*</url-pattern>
有些同志开发web项目的时候,设置或者默认有部署上下文
例如date0906_war_exploded
在浏览器中:http://localhost:8080/date0815_war_exploded/需要这样访问
所以在url-pattern加上了date0906_war_exploded,这是不对的
默认访问路径就有date0906_war_exploded,所以根本无需添加
添加了之后直接访问index等前端页面没有问题,但是
访问Controller中的RequestMapping就会显示404,并提示以下信息:
Description The server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).
服务器无法或将无法处理请求,原因是客户端错误(例如,错误的请求语法,无效的请求消息帧,或欺骗性的请求路由)。
错误的代码2:
<url-pattern>/*</url-pattern>
有些同志又说,那我把/date0906_war_exploded删了,那更不行,汤姆猫一启动就直接报404,并提示以下信息:
Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
源服务器没有找到目标资源的当前表示,或者不愿意公开目标资源的当前表示。
在SpringMVC中/*就是错误的写法,/*会拦截所有路径,拦截之后又返回.jsp文件,又被拦截,就无法正确执行。
详情可以搜搜《SpringMVC url-pattern /*报错》进一步了解。
正确的代码:
<url-pattern>/xxxx/*</url-pattern>
在/*前加上/xxx
这是最简单粗暴的办法了,亲测好用。
还有其他解决方法
比如给dispatcherServlet的拦截加上后缀,例如:*.do;
比如在spring-servlet.xml中加入:
<mvc:default-servlet-handler/>
总结
记录一下。。debug de了一下午,我还一直以为是Mapping注解没扫描到。。。。

本文介绍了SpringMVC配置中常见的错误,特别是DispatcherServlet的url-pattern设置不当导致的404错误。错误示例包括使用项目部署上下文和使用'*'作为url-pattern。正确的做法是在url-pattern前添加特定路径,如'/example/*',并讨论了其他解决方案,如指定后缀或使用<mvc:default-servlet-handler/>。通过正确配置,确保SpringMVC能正确拦截和处理请求。
5330

被折叠的 条评论
为什么被折叠?



