<mvc:default-servlet-handler/>导致controller失效,报404错误

本文详细解析了在SSM框架整合项目中遇到的404错误问题,主要原因是&lt;mvc:default-servlet-handler/&gt;的不当配置导致Controller失效。文章提供了调整配置顺序的解决方案,确保静态资源正常加载同时保持Controller功能。

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

最近在做ssm框架整合的一个小项目时,页面跳转一直有404错误,也没有报错提示。然后一步一步去找,终于发现是<mvc:default-servlet-handler/>的原因。

技术分享图片

如上图所示,如果springmvc的配置文件这样写的话会报错。

<mvc:default-servlet-handler/>这一行代码的意思是使用默认的Servlet来响应静态文件,因为在web.xml中使用了DispatcherServlet截获所有请求的url,包括jsp页面的关于js,css等引入。由于找不到这些静态文件,所以会报404错误。而当配置文件加上这个默认的Servlet时,Servlet会在找不到它的时候去找静态内容。

但是引入这行代码后,Controller就会失效了,准确来说是RequestMapping下面的方法失效了,从而界面无法跳转。在网上看了一些原因后,总结一下就是

<mvc:default-servlet-handler/>将在SpringMVC上下文中定义一个DefaultServletHttpRequestHandler,这个Handler的作用是去Servlet容器查找默认的Servlet来响应静态文件,而这会导致上图配置文件的处理映射器和处理适配器失效,从而Controller失效。


 

解决办法如下图

技术分享图片

<mvc:annotation-driven/>这一行代码的作用是自动注册RequestMappingHandlerMapping和RequestMappingHandlerAdapter这两个bean,所以我把他们注释了。

也就是说这两个bean的顺序一定要在<mvc:default-servlet-handler/>的前面。<mvc:default-servlet-handler/>定义的DefaultServletHttpRequestHandler,是当没有其他mapping可以处理请求时才会执行该handler匹配的mapping,从而去查找静态内容。

同理,你也可以把我的第一张图的<mvc:default-servlet-handler/>这行代码放到两个bean的后面也可解决。

转载于:https://www.cnblogs.com/jdbn/p/11020374.html

shl@DESKTOP-I8CQKFM:/mnt/e/m01.svn/truck/m03.client/Server/tafang-server$ sudo mvn clean package [sudo] password for shl: [INFO] Scanning for projects... [WARNING] [WARNING] Some problems were encountered while building the effective model for com.xkhy.tafang:tafang-server:jar:1.0-SNAPSHOT [WARNING] 'dependencies.dependency.systemPath' for com.cca:cca-agent:jar should not point at files within the project directory, ${project.basedir}/lib/cca-agent-0.0.2.jar will be unresolvable by dependent projects @ line 160, column 16 [WARNING] 'dependencies.dependency.systemPath' for com.cca:cca-core:jar should not point at files within the project directory, ${project.basedir}/lib/cca-core-0.0.39.jar will be unresolvable by dependent projects @ line 167, column 16 [WARNING] 'dependencies.dependency.systemPath' for com.cdi-api:cdi-api:jar should not point at files within the project directory, ${project.basedir}/lib/cdi-api-1.0.jar will be unresolvable by dependent projects @ line 174, column 16 [WARNING] 'dependencies.dependency.systemPath' for org.apache.commons:commons-lang:jar should not point at files within the project directory, ${project.basedir}/lib/commons-lang-2.4.jar will be unresolvable by dependent projects @ line 201, column 16 [WARNING] 'dependencies.dependency.systemPath' for org.eclipse.jdt.core.compiler:ecj:jar should not point at files within the project directory, ${project.basedir}/lib/ecj-3.32.0.jar will be unresolvable by dependent projects @ line 223, column 16 [WARNING] 'dependencies.dependency.systemPath' for com.google.gson:gson:jar should not point at files within the project directory, ${project.basedir}/lib/gson-2.8.9.jar will be unresolvable by dependent projects @ line 240, column 16 [WARNING] 'dependencies.dependency.(groupId:artifactId:type:classifier)' must be unique: redis.clients:jedis:jar -> duplicate declaration of version 4.2.3 @ line 287, column 15 [WARNING] 'dependencies.dependency.systemPath' for com.github.iarellano:json:jar should not point at files within the project directory, ${project.basedir}/lib/json-20211205.jar will be unresolvable by dependent projects @ line 307, column 16 [WARNING] 'dependencies.dependency.(groupId:artifactId:type:classifier)' must be unique: org.projectlombok:lombok:jar -> duplicate declaration of version 1.18.22 @ line 349, column 15 [WARNING] 'dependencies.dependency.(groupId:artifactId:type:classifier)' must be unique: org.codehaus.plexus:plexus-compiler-eclipse:jar -> duplicate declaration of version 2.13.0 @ line 444, column 15 [WARNING] 'dependencies.dependency.systemPath' for com.plexus-testing:plexus-testing:jar should not point at files within the project directory, ${project.basedir}/lib/plexus-testing-1.1.0.jar will be unresolvable by dependent projects @ line 459, column 16 [WARNING] [WARNING] It is highly recommended to fix these problems because they threaten the stability of your build. [WARNING] [WARNING] For this reason, future Maven versions might no longer support building such malformed projects. [WARNING] [INFO] [INFO] -------------------< com.xkhy.tafang:tafang-server >-------------------- [INFO] Building tafang-server 1.0-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-jar-plugin/3.5.0/maven-jar-plugin-3.5.0.pom [WARNING] The POM for org.apache.maven.plugins:maven-jar-plugin:jar:3.5.0 is missing, no dependency information available Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-jar-plugin/3.5.0/maven-jar-plugin-3.5.0.jar [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 4.156 s [INFO] Finished at: 2025-05-21T11:32:08+08:00 [INFO] ------------------------------------------------------------------------ [ERROR] Plugin org.apache.maven.plugins:maven-jar-plugin:3.5.0 or one of its dependencies could not be resolved: Could not find artifact org.apache.maven.plugins:maven-jar-plugin:jar:3.5.0 in central (https://repo.maven.apache.org/maven2) -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginResolutionException shl@DESKTOP-I8CQKFM:/mnt/e/m01.svn/truck/m03.client/Server/tafang-server$
最新发布
05-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值