Summary of three ways SpringMVC accesses static resources

  • 2020-06-12 09:05:18
  • OfStack

If your DispatcherServlet intercepts URL like *.do, there is no problem accessing static resources. If your DispatcherServlet intercepts "/", intercepts all requests and accesses to *.js,*.jpg are also intercepted.

Purpose: normal access to static files, do not find a static file 404.

Scenario 1: Activate defaultServlet of Tomcat to process static files


<servlet-mapping>
  <servlet-name>default</servlet-name>
  <url-pattern>*.jpg</url-pattern>
</servlet-mapping>
<servlet-mapping>
  <servlet-name>default</servlet-name>
  <url-pattern>*.js</url-pattern>
</servlet-mapping>
<servlet-mapping>
  <servlet-name>default</servlet-name>
  <url-pattern>*.css</url-pattern>
</servlet-mapping> 

There are multiple configurations, one for each file
Write before DispatcherServlet, let defaultServlet intercept first, this will not enter Spring, I think the performance is the best.

Tomcat, Jetty, JBoss, and GlassFish Default Servlet name -- "default" Google App Engine Default name of Servlet -- "_ah_default" Resin Default name of Servlet -- "ES37en-ES38en" WebLogic Default name of Servlet -- "FileServlet" WebSphere Default Servlet name -- "SimpleFileServlet"

Option 2: mvc:resources is available after spring3.0.4

Usage of mvc:resources


<!-- Access to static resource files -->
<mvc:resources mapping="/images/**" location="/images/" />

/images /** maps to ResourceHttpRequestHandler for processing, location specifies the location of static resources, which can be in the root directory of web application or in the jar package to compress static resources into jar package. cache-period enables static resources to do web cache

If the following error occurs, it may not be configured < mvc:annotation-driven / > The reason why.

Error WARNING: No mapping found for HTTP request with URI [/ mvc/user/findUser/lisi / 770] in DispatcherServlet with name 'springMVC'

use < mvc:resources/ > key is the URI pattern value of mapping, while value is ResourceHttpRequestHandler. In this way, the access to static resources is cleverly transferred from HandlerMapping to ResourceHttpRequestHandler processing and returned, so it supports the classpath directory, jar package access to static resources.

Another thing to note is that you should not set defaultHandler to SimpleUrlHandlerMapping. ResourceHttpRequestHandler is defaultHandler for static uri, otherwise you cannot process static resources request.

Scheme 3, use < mvc:default-servlet-handler/ >


<mvc:default-servlet-handler/>

The "/ * *" url, registered to SimpleUrlHandlerMapping urlMap, turn by static resource access HandlerMapping to org. springframework. web. servlet. resource. DefaultServletHttpRequestHandler processing and return.

DefaultServletHttpRequestHandler use is the default Servlet for each Servlet container itself.

Additional note: Multiple HandlerMapping execution order issues:

The order attribute value of DefaultAnnotationHandlerMapping is: 0

< mvc:resources/ > The order attribute value for the automatically registered SimpleUrlHandlerMapping is: 2147483646

< mvc:default-servlet-handler/ > The order attribute value for the automatically registered SimpleUrlHandlerMapping is: 2147483647

spring will first execute the order with a small value. When accessing an ES175en.jpg image file, first use DefaultAnnotationHandlerMapping to find the processor, 1 must not be found, we do not call Action ES178en.jpg. Then, the order value is searched in ascending order. Since the last SimpleUrlHandlerMapping matches "/**", 1 must match and then respond to the picture.

Access 1 image, but also layers of matching. I wonder how it works? Another day to do 1 downforce test, Apache 1:1.


Related articles: