Dealing with static resources, I think this is probably the "top priority" of web development after the framework has been built.
Because the display of a website is sure to rely on a variety of resources: scripts, pictures, and so on, then the question is, how to request these static resources in the page?
Do you remember the dispatcherservlet in spring MVC? It is a front controller in spring MVC, and if the configured intercept path is "/", then all requests will be intercepted by it. Access to a static resource is also part of a request, then it is intercepted and then entered into its matching process, and we know that it is matched according to the configuration of the handlermapping. For static resources, the default spring MVC does not have a registration matching rule, and if you request a static resource, you will be reported with a 404 error.
How do I handle requests for static resources?
As described above, we can configure a handlermapping to handle static resources.
<BeanID= "Resourcehttprequesthandler"class= "Org.springframework.web.servlet.resource.ResourceHttpRequestHandler"> < Propertyname= "Locations"value= "classpath:/meta-inf/resources/"></ Property> </Bean> <Beanclass= "Org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> < Propertyname= "Mappings"> <Props> <propKey= "/resources/**">Resourcehttprequesthandler</prop> </Props> </ Property> </Bean>
The Resourcehttprequesthandler is the class that handles the static resource request, and of course you can try writing one yourself if you want.
But now it is less to write simpleurlhandlermapping, the project is to use the annotation configuration, but to put the matching relationship on the annotations
Alternatively, you can use the Resources tab of the MVC namespace to configure
<mapping= "/resources/**" location= "/resources/"/>
In essence, the Resourcehttprequesthandler is registered with the simpleurlhandlermapping.
Is there another way to handle static resource requests?
Spring MVC also provides a configuration entry: Mvc:default-servlet-handler
This tag is valid for Dispatcherservlet that match the "/" rule (because other matching rules do not normally intercept static resources). It configures the "/**" interception rule and the lowest matching priority for the Defaultservlethttprequesthandler.
Defaultservlethttprequesthandler handles the request and forwards it all to the container's defaultservlet. Therefore it must be the lowest priority in the handlermapping. If you use <mvc:annotation-driven> or you use a custom handlermapping instance, make sure their order value is smaller than Defaultservlethttprequesthandler (Integer.max).
It is also important to note that the Defaultservlet of the search container here is the name rather than the path. So first of all to understand the name of the container defaultservlet, of course, the name of the general mainstream container is not required, such as Tomcat, Jetty, JBoss, and GlassFish. If you use containers very well, you may need to specify them manually:
<default-servlet-name= "Mycustomdefaultservlet"/>
This way is also dependent on the defaultservlet of the container, then we can directly use the container's defaultservlet to handle the static resource request, instead of first through the spring MVC to forward it? (much better than performance), the answer is yes.
For example, if we put the resource files in the Resouces directory, we only need to configure them in Web. xml:
< servlet-mapping > < Servlet-name >default</servlet-name> <Url-pattern >/resource/*</url-pattern></ Servlet-mapping>
and put it at the front of all the servlets (to make it match first), so it should be better in performance
However, there will be a problem, that is, unable to access the resource files under Classpath, look at the configuration of Tomcat Defaultservlet, there seems to be no place to specify the directory.
Therefore, in summary, the best performance should be the direct use of the container defaultservlet, let it first intercept the static resource requests, so that the subsequent forwarding and other operations, improve performance, but unable to access the resource files under Classpath. The mvc:resources tag can be easily configured to match rules and resource file path, it should be said to be the simplest and quickest way, of course, this is probably the original idea of MVC namespace design.
In addition, if you want to combine the two, you can try to write a servlet to deal with, but estimated to be difficult and troublesome.
Go Several ways to handle static resources in Spring MVC