Spring MVC learning ------------- access to static files

Source: Internet
Author: User

Spring MVC learning ------------- access to static files

How can I access static files, such as jpg, js, and css?

How to intercept a URL with a suffix such as *. do in your DispatcherServlet does not cause access to static resources.
If your DispatcherServlet intercepts/and intercepts all requests to implement the REST style, the access to static files such as *. js and *. jpg will also be blocked.

We need to solve this problem.

 

Purpose: To access a static file normally. If the static file cannot be found, the Error 404 is returned.


Solution 1: Activate Tomcat's defaservlet servlet to process static files

Xml Code
  1. Default
  2. *. Jpg
  3.  
  4. Default
  5. *. Js
  6.  
  7. Default
  8. *. Css
  9.  
  10. You need to configure multiple, one for each file

    Write it in front of DispatcherServlet and let defaservlet servlet intercept the request first so that the request will not enter Spring. I think the performance is the best.

     

    Tomcat, Jetty, JBoss, and GlassFish default Servlet name -- default
    Name of the default Servlet provided by Google App Engine -- _ ah_default
    Default Servlet name provided by Resin -- resin-file
    The default Servlet name provided by WebLogic-FileServlet
    SimpleFileServlet, the default Servlet name that comes with WebSphere

     

     


    Solution 2: mvc: resources is provided in spring3.0.4 and later versions. Usage:

    Xml Code

    1. /Images/** ing to ResourceHttpRequestHandler for processing. location specifies the location of static resources. it can be under the web application root directory and in the jar package, so that static resources can be compressed into the jar package. Cache-period allows static resources to be used for web cache

      The following error may be caused by no configuration. .
      Error WARNING: No mapping found for HTTP request with URI [/mvc/user/findUser/lisi/770] in DispatcherServlet with name 'springmvc'

       

      Use And register the URI of simpleing to 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 converted from HandlerMapping to ResourceHttpRequestHandler for processing and returning. Therefore, the classpath directory and the access to static Resources in the jar package are supported.
      In addition, do not set defaultHandler for SimpleUrlHandlerMapping. Because the defaultHandler for static uri is ResourceHttpRequestHandler,
      Otherwise, the static resources request cannot be processed.

       

       

      Solution 3: Use

      Xml Code
      1.  

        The/** url will be registered to the urlMap of SimpleUrlHandlerMapping, and the access to static resources will be transferred from HandlerMapping to org. springframework. web. servlet. resource. DefaultServletHttpRequestHandler for processing and returning.
        DefaultServletHttpRequestHandler is the default Servlet of each Servlet container.

         

         

        Note: The execution sequence of HandlerMapping is as follows:

        The attribute value of order DefaultAnnotationHandlerMapping is: 0.

        The order attribute value of automatically registered SimpleUrlHandlerMapping is: 2147483646

         

        The order attribute value of automatically registered SimpleUrlHandlerMapping is: 2147483647

         

         

        Spring will first execute the order value smaller. When copying an a.jpg image file, you must first use DefaultAnnotationHandlerMapping to find the handler. It must be because we didn't call the.jpg Action. Then search by order value in ascending order. Because the last SimpleUrlHandlerMapping matches/**, the image will be matched and then the image will be responded.


         

        To access an image, perform layer-by-layer matching. What is the performance?

        Finally, solution 2 and solution 3 use the Interceptor to access static resources. If you implement permission check in interception, you must filter these requests for static files.

        How to intercept URLs such as *. do in your DispatcherServlet does not save the above problem. There is a suffix for convenience.

         

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.