Three ways to deal with springmvc static resources

Option 1: Activate Tomcat's defaultServlet 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>  
To configure multiple, one for each file configuration should   
be written in front of 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 Servlet Name -- "_ah_default"
Resin Default Servlet Name -- "resin-file"
WebLogic Default Servlet Name -- "FileServlet "

WebSphere default servlet name -- "SimpleFileServlet"



Option 2: mvc:resources 


<mvc:resources usage:

<!--Access to static resource files-->
<mvc:resources mapping="/images/**" location="/images/" />
  /images /** is mapped to ResourceHttpRequestHandler for processing, and location specifies the location of static resources. It can be in the root directory of the web application or in the jar package, so that the static resources can be compressed into the jar package. cache-period can make static resources web cache  


If the following error occurs, it may be the reason that <mvc:annotation-driven /> is not configured. 
Error WARNING: No mapping found for HTTP request with URI [/mvc/user/findUser/lisi/770] in DispatcherServlet with name 'springMVC'


Using the <mvc:resources/> element, the URI of the mapping is registered in the urlMap of SimpleUrlHandlerMapping, the
key is the URI pattern value of the mapping, and the value is the ResourceHttpRequestHandler,
so that the access to the static resources is cleverly transferred from the HandlerMapping to the ResourceHttpRequestHandler for processing and processing. Return, so it supports the access of static resources in the classpath directory and jar package.
Another point to note is, do not set defaultHandler for SimpleUrlHandlerMapping. Because the defaultHandler for static uri is ResourceHttpRequestHandler,
otherwise it cannot process static resources request.


Option three, use < mvc:default-servlet-handler/>

<mvc:default-servlet-handler/>
The "/**" url will be registered in the urlMap of SimpleUrlHandlerMapping, and the access to static resources will be transferred from HandlerMapping to org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler for processing and return.
The use of DefaultServletHttpRequestHandler is the default of each servlet container. Servlet.


Supplementary note: The execution order of multiple HandlerMappings:

The value of the order attribute of DefaultAnnotationHandlerMapping is: 0

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

<mvc:default-servlet-handler/> The value of the automatic registration The value of the order property of SimpleUrlHandlerMapping is: 2147483647

Spring will execute the order value smaller first. When accessing an a.jpg image file, first look for the processor through DefaultAnnotationHandlerMapping, it must not be found, we do not have an Action called a.jpg. Then search in ascending order of the order value. Since the last SimpleUrlHandlerMapping matches "/**", it will definitely match, and then respond to the picture.

To access a picture, you have to go through layers of matching. I really do not know how the performance? Do a stress test another day and compare it with Apache.

Finally, let me explain how your DispatcherServlet intercepts URLs such as *.do, and the above problems do not exist.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325498032&siteId=291194637