How to access static files, such as Jpg,js,css?

Source: Internet
Author: User

If your dispatcherservlet blocks a URL with a suffix such as "*.do", there is no problem accessing static resources.

If your dispatcherservlet intercepts "/", in order to implement the rest style, all requests are intercepted, and access to static files such as *.js,*.jpg is intercepted. "The current project is mostly like this."

We have to solve this problem.

Purpose: The static file can be accessed normally, and no static file 404 can be found.

Here's a reminder: How the static files in Web-inf are set is not accessible. All this is said in the web-inf outside the static file, mainly is web-root under.


Scenario One: Activate Tomcat's defaultservlet to handle static files "in Tomcat/conf/web.xml files"

  1. <servlet-mapping>
  2. <servlet-name>default</servlet-name>
  3. <url-pattern>*.jpg</url-pattern>
  4. </servlet-mapping>
  5. <servlet-mapping>
  6. <servlet-name>default</servlet-name>
  7. <url-pattern>*.js</url-pattern>
  8. </servlet-mapping>
  9. <servlet-mapping>
  10. <servlet-name>default</servlet-name>
  11. <url-pattern>*.css</url-pattern>
  12. </servlet-mapping>
  13. To configure multiple, each file is configured with a

To write in front of the Dispatcherservlet, let defaultservlet first intercept the request, so the request will not enter spring, I think the performance is the best.

Tomcat, Jetty, JBoss, and GlassFish the name of the default servlet that comes with--"default"
Google App Engine comes with the name of the default servlet-"_ah_default"
The name of the default servlet that Resin comes with--"resin-file"
The name of the default servlet that WebLogic comes with--"Fileservlet"
The name of the default servlet that comes with WebSphere --"Simplefileservlet"


Scenario two: After the spring3.0.4 version provides the mvc:resources, using the method:

    1. <!--access to static resource files--
    2. <mvc:resources mapping="/images/**" location="/images/" />

/images/** This is the location of the url,location is the file, as configured above/images/only in the root directory/images/the graphics file is valid. However, once you have configured the information in the scenario three, you will be able to access it. Programme II and Programme III, two higher priorities, will be addressed first.

But if it is configured to <mvc:resources mapping="/**" location="/" /> No scenario three, no matter where the file is accessed, As long as not in the Web-inf, can access to.
The/images/** maps to Resourcehttprequesthandler for processing, and location specifies the position of the static resource. Can be a Web application root directory, inside the jar package, so that the static resources can be compressed into the jar package. Cache-period allows static resources to be Web cache

If the following error occurs, there may be a reason for not configuring <mvc:annotation-driven/>.
Error warning:no mapping found for HTTP request with URI [/mvc/user/finduser/lisi/770] in Dispatcherservlet with Name ' Spring MVC '

Using the <mvc:resources/> element, register the URI of the mapping in the UrlMap of Simpleurlhandlermapping,
Key is the URI pattern value of mapping, and 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 and the access of static resources within the JAR package.
It is also important to note that you should not set DefaultHandler for simpleurlhandlermapping. Because the DefaultHandler to the static URI is Resourcehttprequesthandler,
Otherwise, the static resources request cannot be processed.

Scenario three, using <mvc:default-servlet-handler/>

    1. <mvc:default-servlet-handler/>

The "/**" URL will be registered in the UrlMap of Simpleurlhandlermapping, The access to the static resource is transferred from handlermapping to Org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler processing and returned.
Defaultservlethttprequesthandler use is the default servlet for each servlet container.

Additional note: The order of execution of multiple handlermapping issues:

The Defaultannotationhandlermapping Order property value is: 0

<mvc:resources/> autoenrollment simpleurlhandlermapping The Order property value is:2147483646

<mvc:default-servlet-handler/> simpleurlhandlermapping 's Order property value for autoenrollment is: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, because we did not call a.jpg action. And then by the order value in ascending search, because the last simpleurlhandlermapping is matching "/**", so it will match, you can respond to the picture.

Access to a picture, but also to go layer-by-layer matching. Not sure how it's performing?

Finally, the second scenario, scenario three, when accessing static resources, if there is a matching (approximate) Total interceptor, it will go to the interceptor. If you implement permission checking in interception, be careful to filter these requests for static files.

How your Dispatcherservlet intercepts *.do such a URL suffix does not save the above problem. There is also a suffix convenient.

How to access static files, such as Jpg,js,css?

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.