Web SpringMVC项目404 Description The server cannot or will not process the request due to something th
问题可能出现在web.xml中的url-pattern,在web项目配置SpringMVC提供的DispatcherServlet时,拦截路径写错了。以下是正确的代码截图:以下是正确的代码:<servlet><servlet-name>DispatcherServlet</servlet-name><servlet-class>org.springf
问题可能出现在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注解没扫描到。。。。
昇腾计算产业是基于昇腾系列(HUAWEI Ascend)处理器和基础软件构建的全栈 AI计算基础设施、行业应用及服务,https://devpress.csdn.net/organization/setting/general/146749包括昇腾系列处理器、系列硬件、CANN、AI计算框架、应用使能、开发工具链、管理运维工具、行业应用及服务等全产业链
更多推荐

所有评论(0)