Static resource access issues in Spring MVC restful building

Source: Internet
Author: User

When building a spring MVC restful style application, as in Web. xml:

<span style= "Font-family:microsoft yahei;font-size:18px;" ><servlet><servlet-name>story</servlet-name><servlet-class> org.springframework.web.servlet.dispatcherservlet</servlet-class><load-on-startup>1</ load-on-startup></servlet><servlet-mapping><servlet-name>story</servlet-name>< Url-pattern>/</url-pattern></servlet-mapping></span>

Interception of all requests, including, of course, request interception of static resources, such as a reference to a page facing a image,css,js file, does not define the appropriate controller to respond to these requests, so these requests are usually not completed.

In this case, we should think of a problem: in Tomcat, only the servlet can handle the request, and even the JSP will be compiled into a servlet. Note that in a servlet container, these resources are handled by the servlet. However, different servlet container/application servers have a small name for the servlet that handles these static resources:
Tomcat, Jetty, JBoss, and GlassFish: The default Servlet name is "default";
Google App Engine: The default Servlet name is "_ah_default";
Resin: The default Servlet name is "Resin-file";
WebLogic: The default Servlet name is "Fileservlet";
WebSphere: The default Servlet name is "Simplefileservlet";


There are several common solutions to these problems:

Scenario One: Activate Tomcat's defaultservlet to handle static resources

<span style= "Font-family:microsoft yahei;font-size:18px;" ><servlet-mapping><servlet-name>default</servlet-name><url-pattern>*.css</ 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>*.jpg</url-pattern></servlet-mapping></span>

Each type of static resource needs to be configured with a servlet-mapping, and at the same time, written in front of Dispatcherservlet, let Defaultservlet intercept first. (Do not be sure to put in Dispatcherservlet before you need to verify)
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.


Scenario Two: Spring 3.0.4 future Version provides <mvc:resources/>

<!--handle static resources--

<span style= "Font-family:microsoft yahei;font-size:18px;" ><!--uploaded images cached for 1 months, other js,css,img resources cached one year--  <mvc:resources mapping= "/res/**" location= "/res/" Cache-period= "2592000"/><mvc:resources mapping= "/css/**" location= "/css/" cache-period= "31536000"/>< Mvc:resources mapping= "/js/**" location= "/js/" cache-period= "31536000"/><mvc:resources mapping= "/img/**" location= "/img/" cache-period= "31536000"/></span>

mapping maps to Resourcehttprequesthandler for processing, location specifies the position of the static resource, which can be a Web application The root directory, inside the jar package, to compress the static resources into the jar package. Cache-period can make a static resource Web cache.

using the <mvc:resources/> element, the URI of the mapping is registered to the urlmap of simpleurlhandlermapping, and the key is the URI pattern value of mapping, and Value is Resourcehttprequesthandler, which subtly transfers access to the static resource from handlermapping to Resourcehttprequesthandler processing and returns, so it supports Classpath directory, access to static resources within the JAR package.


Scenario Three: use of <mvc:default-servlet-handler/>
<mvc:default-servlet-handler/> will register the "/**" URL to the UrlMap of simpleurlhandlermapping, transferring access to the static resource from handlermapping to Org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler processed and returned. Defaultservlethttprequesthandler use is the default servlet for each servlet container.

The default value for the order of the handlermapping mentioned above is supplemented by the following instructions:
defaultannotationhandlermapping:0
<mvc:resources/> Automatic registration of simpleurlhandlermapping:2147483646
<mvc:default-servlet-handler/> Automatic registration of simpleurlhandlermapping:2147483647

Spring executes a smaller order value first. When access to a a.jpg picture file, first through the defaultannotationhandlermapping to find the processor, must not be found, we did not call A.jpg Controller. And then the order value in ascending, because the last simpleurlhandlermapping is a match "/**", so will definitely match, and then respond to the picture.
In Spring MVC, a picture is accessed, and layers are matched. Performance is certainly not where to go. Not only Spring MVC, but Struts, after all, survive in the servlet container, so long as the servlet container handles these static resources, it is necessary to read these resources into the JVM's memory area. So, when dealing with static resources, we usually add Apache or Nginx to the front end.
In addition, 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 general, if we use spring MVC to develop our application, the common solution is to solve the problem, and often use <c:url value= ""/> to refer to some resources, However, a resource referenced in CSS, such as Body{background-image:url (. contextpath), cannot be requested because it does not have a. /image/bg.jpg)}, this can be combined with scenario one, in Web. xml:

<span style= "Font-family:microsoft yahei;font-size:18px;" ><servlet-mapping><servlet-name>default</servlet-name><url-pattern>*.jpg</ Url-pattern></servlet-mapping></span>

Note the:<c:url> tag function is to format a URL address as a string and save it in a variable. It has URL auto-rewrite functionality. The URL specified by value can be either a URL to the current project or a URL to another Web project. However, the context property is required. You can also add parameters that need to be passed.

Property:
var: variable name;
Value: the URL to format;
Scope: Scope range, default is page;
Context: other engineering paths;

<c:url var= "Urlstr" value= "/user.jsp" ><c:param name= "id" value= "111"/></c:url><c:url var= " Urlstr "value="/user.jsp "context="/project "/><!--other web apps in the same container--><c:out value=" ${urlstr} "/><a href= "${urlstr}" > Test </a>

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Static resource access issues in Spring MVC restful building

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.