The difference between blocking/and intercepting/* in spring-no access to the returned JSP-access to static resources (JPG,JS, etc.) __php

Source: Internet
Author: User

As we all know, in spring-based application, you need to add the following similar configuration information to the Web.xml:

<listener>  
    <listener-class>  
        Org.springframework.web.context.ContextLoaderListener  
    < /listener-class>  
</listener>  

<!--   Spring MVC Servlet-->  

<servlet>  
    < servlet-name>servletname</servlet-name>  
    <servlet-class>  
        Org.springframework.web.servlet.DispatcherServlet  
    </servlet-class>  
    <load-on-startup>1 </load-on-startup>  
</servlet>  

<servlet-mapping>  
    <servlet-name> Servletname</servlet-name>   
    <strong><url-pattern>/</url-pattern></strong>  
</servlet-mapping>

The special emphasis here is/is used/, rather than/, if used/, then the request can be forwarded via Dispatcherservlet to the corresponding action or controller, but the returned content, such as the returned JSP will be intercepted again, This results in a 404 error, that is, the JSP is not reachable. So if you find that there are always 404 errors in the future, don't forget to check/the configuration is/*.

Second, in fact, Spring's servlet interceptor match rule (ie ... Can be defined by itself, for example: when mapped to @requestmapping ("/user/add")

1, intercept. Do,. htm, for example:/user/add.do
This is the most traditional way, the simplest and most practical. does not cause static files (jpg,js,css) to be blocked.

2, interception/, for example:/user/add
Can realize the restful style that is now very popular. Many internet types of applications like this style of URLs.
Disadvantage: will cause static file (JPG,JS,CSS) is blocked after the normal display. If you want to achieve restful style, things are just a bit of a hassle. There is a solution behind it that is simple.

3, interception/*, this is a wrong way, the request can go to the action, but go to the JSP is intercepted again, can not access to JSP.

Third, how to access to static files, such as Jpg,js,css?

If your dispatcherservlet intercepts URLs with suffixes such as "*.do", there is no question of accessing static resources.

If your dispatcherservlet intercepts "/" and intercepts all requests in order to achieve restful style, access to static files such as. Js,.jpg is intercepted.

We have to solve this problem.

Objective: Static file can be accessed normally, no static file 404 can be found.

Scenario One: Activate Tomcat's defaultservlet to process static files

<servlet-mapping>     
    <servlet-name>default</servlet-name>    
    <url-pattern>*.jpg</ 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>*.css</ Url-pattern>        
</servlet-mapping>

Features: 1. To configure multiple, each file is configured with one.
2. To write in front of the Dispatcherservlet, let Defaultservlet first intercept the request, so that the request will not enter spring.
3. High performance.
Note:
Name of the default servlet with Tomcat, Jetty, JBoss, and GlassFish – "Default"
Google App Engine The name of the default servlet-"_ah_default"
Resin the name of the default servlet-"Resin-file"
WebLogic the name of the default servlet-"Fileservlet"
Name of the default servlet with WebSphere-"Simplefileservlet"

Programme II: Mvc:resources is available after the spring3.0.4 version, using the following methods:
XML code Collection Code


images/** maps to Resourcehttprequesthandler for processing, location specifies the location of the static resource. Can be the Web application root directory, jar package, so you can compress the static resources into the jar package. Cache-period can enable static resources for Web cache

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

Using elements, register the URI of the mapping into the simpleurlhandlermapping UrlMap,
The key is a mapping URI pattern value, and value is Resourcehttprequesthandler,
This cleverly transfers access to static resources from handlermapping to Resourcehttprequesthandler processing and returns, so it supports the Classpath directory, access to static resources within the JAR package.
Another point to note is that do not set DefaultHandler for simpleurlhandlermapping. Because the defaulthandler of the static URI is Resourcehttprequesthandler,
Otherwise, the static resources request cannot be processed.

Programme III, using
XML code Collection Code

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

Supplemental Note: Multiple handlermapping execution order issues:
Defaultannotationhandlermapping's order attribute value is: 0

The simpleurlhandlermapping order attribute value for autoenrollment is: 2147483646

The simpleurlhandlermapping order attribute value for autoenrollment is: 2147483647

Spring will first perform a smaller order value. When access to a a.jpg picture file, first through the defaultannotationhandlermapping to find the processor, must not be found, because we do not call the a.jpg action. And then in order value ascending search, because the last simpleurlhandlermapping is matching "/**", so will definitely match, you can respond to the picture.
Finally, the second, Scheme III in Access to static resources, if there is a matching (approximate) Total interceptor, will go to the interceptor. If you implement a permission check in the interception, be careful to filter these requests for static files.

If your dispatcherservlet intercepts a URL suffix such as *.do, you won't be saving the problem. Still have the suffix convenient.

Related Article

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.