<mvn:default-servlet-handler/>标签作用

servlet在找页面时,走的是dispatcherServlet路线。找不到的时候会报404

加上这个默认的servlet时候,servlet在找不到的时候会去找静态的内容。


另外一篇文章介绍了访问静态文件的几种方法:

访问到静态的文件,如jpg,js,css

  

如何你的DispatcherServlet拦截 *.do这样的URL,就不存在访问不到静态资源的问题。如果你的DispatcherServlet拦截“/”,拦截了所有的请求,同时对*.js,*.jpg的访问也就被拦截了。

目的:可以正常访问静态文件,不要找不到静态文件报404。


方案一:激活Tomcat的defaultServlet来处理静态文件
[html]  view plain copy
  1. <servlet-mapping>     
  2.     <servlet-name>default</servlet-name>    
  3.     <url-pattern>*.jpg</url-pattern>       
  4. </servlet-mapping>      
  5. <servlet-mapping>         
  6.     <servlet-name>default</servlet-name>      
  7.     <url-pattern>*.js</url-pattern>      
  8. </servlet-mapping>      
  9. <servlet-mapping>          
  10.     <servlet-name>default</servlet-name>         
  11.     <url-pattern>*.css</url-pattern>        
  12. </servlet-mapping>      
  13. 要配置多个,每种文件配置一个  

要写在DispatcherServlet的前面, 让 defaultServlet先拦截,这个就不会进入Spring了 


Tomcat, Jetty, JBoss, and GlassFish  默认 Servlet的名字 -- "default"
Google App Engine 默认 Servlet的名字 -- "_ah_default"
Resin 默认 Servlet的名字 -- "resin-file"
WebLogic 默认 Servlet的名字  -- "FileServlet"
WebSphere  默认 Servlet的名字 -- "SimpleFileServlet" 


方案二: 在spring3.0.4以后版本提供了mvc:resources 
mvc:resources 的使用方法:
[html]  view plain copy
  1. <!-- 对静态资源文件的访问 -->      
  2. <mvc:resources mapping="/images/**" location="/images/" />    

[html]  view plain copy
  1. <span style="font-weight: normal;">mapping<span style="font-family: Arial; line-height: 1.5em; ">映射到ResourceHttpRequestHandler进行处理  
  2. location指定静态资源的位置.可以是web application根目录下、jar包里面,这样可以把静态资源压缩到jar包中  
  3. cache-period 可以使得静态资源进行web cache </span></span>  

如果出现下面的错误,可能是没有配置<mvc:annotation-driven />的原因。 
报错WARNING: No mapping found for HTTP request with URI [/mvc/user/findUser/lisi/770] in DispatcherServlet with name 'springMVC'

 

使用<mvc:resources/>元素,把mapping的URI注册到SimpleUrlHandlerMapping的urlMap中,key为mapping的URI pattern值,而value为ResourceHttpRequestHandler,这样就巧妙的把对静态资源的访问由HandlerMapping转到ResourceHttpRequestHandler处理并返回,所以就支持classpath目录,jar包内静态资源的访问.另外需要注意的一点是,不要对SimpleUrlHandlerMapping设置defaultHandler.因为对static uri的defaultHandler就是ResourceHttpRequestHandler,否则无法处理static resources request.


方案三 ,使用<mvc:default-servlet-handler/>
[html]  view plain copy
  1. <mvc:default-servlet-handler/>  

会把"/**" url,注册到SimpleUrlHandlerMapping的urlMap中,把对静态资源的访问由HandlerMapping转到org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler处理并返回.DefaultServletHttpRequestHandler使用就是各个Servlet容器自己的默认Servlet.

 

补充说明:多个HandlerMapping的执行顺序问题:

DefaultAnnotationHandlerMapping的order属性值是:0
<mvc:resources/ >自动注册的 SimpleUrlHandlerMapping的order属性值是: 2147483646

<mvc:default-servlet-handler/>自动注册 的SimpleUrlHandlerMapping 的order属性值是: 2147483647

spring会先执行order值比较小的。当访问一个a.jpg图片文件时,先通过 DefaultAnnotationHandlerMapping 来找处理器,一定是找不到的,我们没有叫a.jpg的Action。再按order值升序找,由于最后一个 SimpleUrlHandlerMapping 是匹 "/**"的,所以一定会匹配上,再响应图片。


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
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值