Turn around! SPRINGMVC three ways to access static resources

Source: Internet
Author: User

If your dispatcherservlet intercepts URLs such as *.do, there is no problem accessing static resources. If your dispatcherservlet intercepts "/", all requests are intercepted, and access to *.js,*.jpg is intercepted.

Problem reason: The culprit is the configuration of the Dispatcherservlet request URL mapping for spring under Web. XML, which is configured as follows:

 
  1. <servlet>
  2. <servlet-name>spring</servlet-name>
  3. <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class >
  4. <load-on-startup>1</load-on-startup>
  5. </servlet>
  6. <servlet-mapping>
  7. <servlet-name>spring</servlet-name>
  8. <span style="Background-color: #ffff33"><url-pattern>/ </url-pattern>
  9. </span> </servlet-mapping>

Analysis of the cause:<servlet-mapping> <url-pattern>/</url-pattern> put all the requests to spring to deal with, And all available request URLs are configured in Constroller with annotations like @requestmapping (value = "/login/{user}", method = Requestmethod.get), In this way, access to static resources such as Js/css/jpg/gif will not be available.

Purpose: Normal access to static files, do not find static file 404.
Scenario One: Activate Tomcat's defaultservlet to handle static files

Add the following configuration in Web. xml:

 
  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>

To configure multiple, each file is configured with one.
To write in front of the Dispatcherservlet, let Defaultservlet first intercept, this will not enter spring, I think the performance is the best bar.

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

Scenario two: After the spring3.0.4 version provided the Mvc:resources

How to use <mvc:resources>:

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

/images/** maps to Resourcehttprequesthandler for processing, location specifies the position of the static resource. It can be a Web application root, a jar package, which compresses the static resources into the jar package. Cache-period can make a static resource 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, 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 Res Ourcehttprequesthandler, so cleverly to the static resource access from handlermapping to Resourcehttprequesthandler processing and return, so support Classpath directory, jar Access to static resources within the package. It is also important to note that you do not set DefaultHandler on simpleurlhandlermapping. The static resources request cannot be processed because the defaulthandler of the static URI is Resourcehttprequesthandler.

Mapping: Mapping
Location: Local resource path, note must be the path under the WebApp root directory.
Two *, which represents all URLs under Map resources/, including sub-paths (that is, multiple/)
Trap:
The configured location must be the WebApp root directory, and if you put the resource directory under Webapp/web-inf, the access will fail. This problem is often committed.
Error mode:

Web-inf Directory role
Web-inf is a secure directory for Java Web applications. The so-called security is the client cannot access, only the server can access the directory.
If you want to access the files directly in the page, you must map the files you want to access through the Web. xml file to access them.
Of course, you must modify the resources mappings if you want to place them in web-inf, such as:


 
    1. <span style="Background-color: #ffff33"><mvc:resources mapping="/js/ * * " location="/web-inf/js/" /></span>

recommended way:The directory structure of the MAVEN project is as shown.

Scenario three, using <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.

Additional note: The order of execution of multiple handlermapping issues:
The Defaultannotationhandlermapping Order property value is: 0
<mvc:resources/> Auto-registered Simpleurlhandlermapping's Order property value is: 2147483646
The value of the Order property for the <mvc:default-servlet-handler/> autoenrollment simpleurlhandlermapping 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, we do not call a.jpg Action. And then the order value in ascending, because the last simpleurlhandlermapping is a match "/**", so will definitely match, and then respond to the picture.

Finally, how do you dispatcherservlet intercept *.do such a URL, do not save the above problem.

Turn around! SPRINGMVC three ways to access static resources

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.