The difference between interception/ and interception/* in Spring and the solution for not intercepting resource files

1. We all know that in a Spring-based Application, we need to add configuration information similar to the following in 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>
  <url-pattern>/</url-pattern>
  </servlet-mapping>
  It should be emphasized here that <url-pattern>/</url-pattern> uses / instead of /*. If /* is used, the request can be forwarded to the corresponding Action or Controller through DispatcherServlet. However, the returned content, such as the returned jsp, will be intercepted again, resulting in a 404 error , that is, the jsp cannot be accessed. So if you find that there are always 404 errors in the future, don't forget to check whether the configuration of <url-pattern>/</url-pattern> is /*.

  2. In fact, Spring's Servlet interceptor matching rules (ie <url-pattern>...</url-pattern> ) can be defined by themselves, for example: when the mapping is @RequestMapping("/user/add")

  1. Intercept *.do, *.htm, for example: /user/add.do

  This is the most traditional way, the easiest and the most practical. Will not cause static files (jpg, js, css) to be blocked.

  2. Intercept /, for example: /user/add

  It can implement the REST style that is very popular now. Many Internet-type applications like this style of URL.

  Disadvantages: It will cause static files (jpg, js, css) to be blocked and not displayed properly. If you want to achieve REST style, things are a little more troublesome. The solution behind is fairly simple.

  3. Intercept /*, this is a wrong way, the request can go to the Action, but it is intercepted again when it goes to the jsp, and the jsp cannot be accessed.

  3. How to access static files, such as jpg, js, css?

  If your DispatcherServlet intercepts suffixed URLs such as "*.do", there is no problem of not being able to access static resources.

  If your DispatcherServlet intercepts "/", in order to achieve the REST style, all requests are intercepted, then the access to static files such as *.js, *.jpg will be intercepted at the same time.

  We are going to solve this problem.

  Purpose: The static files can be accessed normally, but the static files cannot be found and a 404 is reported.

  Option 1: 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, configure one for each file.

  2. To be written in front of DispatcherServlet, let defaultServlet intercept the request first, so that the request will not enter Spring.

  3. High performance.

  Remark:

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

  Option 2: mvc:resources is provided in spring 3.0.4 and later, the usage method:

  <!-- Access to static resource files -->
  <mvc:resources mapping="/images/**" location="/images/" />
  images/** is mapped to ResourceHttpRequestHandler for processing, and location specifies the location of static resources. It can be in the root directory of the web application or in the jar package, so that the static resources can be compressed into the jar package. cache-period allows static resources to be web cached

  
  If the following error occurs, it may be the reason that <mvc:annotation-driven /> is not configured.
  报错WARNING: No mapping found for HTTP request with URI [/mvc/user/findUser/lisi/770] in DispatcherServlet with name 'springMVC'

  

  Use the <mvc:resources/> element to register the URI of the mapping to the urlMap of SimpleUrlHandlerMapping,
  The key is the URI pattern value of the mapping, and the value is the ResourceHttpRequestHandler,
  In this way, the access to static resources is cleverly transferred from HandlerMapping to ResourceHttpRequestHandler for processing and return, so it supports the access of static resources in the classpath directory and jar package.
  Another point to note is, do not set defaultHandler for SimpleUrlHandlerMapping. Because the defaultHandler for static uri is ResourceHttpRequestHandler,
  Otherwise, the static resources request cannot be processed.

  Option 3, use <mvc:default-servlet-handler/>

  <mvc:default-servlet-handler/>
  The "/**" url will be registered in 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 return.
  DefaultServletHttpRequestHandler uses the default Servlet of each Servlet container.

  Supplementary note: The execution order of multiple HandlerMappings:

  The order property value of DefaultAnnotationHandlerMapping is: 0

  The value of the order attribute of the automatically registered SimpleUrlHandlerMapping of < mvc:resources/ > is: 2147483646

  The value of the order attribute of the automatically registered SimpleUrlHandlerMapping of <mvc:default-servlet-handler/> is: 2147483647

  Spring will execute the smaller order value first. When accessing an a.jpg image file, first find the processor through DefaultAnnotationHandlerMapping, it must not be found, because we do not have an Action called a.jpg. Then search in ascending order of the order value. Since the last SimpleUrlHandlerMapping matches "/**", it will definitely match, and the image can be responded to. To access a picture, you have to go through layers of matching. I do not know how the performance?

  Finally, let me explain that when the two and three schemes access static resources, if there is a matching (approximate) total interceptor, the interceptor will be used. If you implement permission checking in interception, be careful to filter these requests for static files.

  If your DispatcherServlet intercepts URL suffixes such as *.do, the above problems will not exist. There is still a suffix for convenience.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326995971&siteId=291194637