Spring MVC access static file _ Power node Java College collation

  • 2020-09-16 07:27:40
  • OfStack

If your DispatcherServlet intercepts URL with suffix "*.do ", there is no problem accessing static resources.

If your DispatcherServlet intercepts "/", and in order to implement the REST style, intercepts all requests, then access to static files such as *.js,*.jpg is also blocked.

We're going to solve this problem.

Purpose: Normal access to static files, can not find a static file 404.

Scenario 1: Activate Tomcat's defaultServlet to process static files

Xml code


<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 and have defaultServlet intercept the request so it doesn't go into Spring, which I think is the best performance.

Tomcat, Jetty, JBoss, and GlassFish default Servlet name -- "default"
Google App Engine default Servlet name -- "_ah_default"
Resin comes with the default name of Servlet -- "ES53en-ES54en"
Name of the default Servlet that comes with WebLogic -- "FileServlet"
WebSphere comes with the default name of Servlet -- "SimpleFileServlet"

Scheme 2: mvc:resources is provided after spring3.0.4, using method:
Xml code


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

/images/** maps to ResourceHttpRequestHandler for processing, and location specifies the location of the static resource. It can be web application root directory, jar package, which can 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/ > Element, mapping URI to SimpleUrlHandlerMapping urlMap,key mapping URI pattern value, value ResourceHttpRequestHandler value, this clever transfer of access to static resources from HandlerMapping to ResourceHttpRequestHandler processing and return, so support classpath directory,jar package access to static resources.

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

Option 3: Use < mvc:default-servlet-handler/ >
Xml code


<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 notes: The execution order of multiple HandlerMapping:
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 ES195en. jpg image file, first use DefaultAnnotationHandlerMapping to find the processor, 1 must not be found, because we do not have Action named a. jpg. Then, it is searched in ascending order according to order value. Since the last SimpleUrlHandlerMapping matches "/**", 1 must match, and the image can be responded.

Access 1 image, but also layers of matching. How about the performance?

Finally, in scenario 2. Scenario 3, when accessing a static resource, if there is a matching (approximate) master interceptor, the interceptor is removed. If you implement permission checks in interception, be careful to filter requests to static files.

How your DispatcherServlet block *.do such as the URL suffix, there is no problem with the above. It's still convenient to have suffixes.


Related articles: