SpringMVC interception solve the problem of static resources

Elegant REST-style URL resources such as .html or do not want to bring .do suffix. Since the early Spring MVC does not handle static resources, so the configuration request DispatcherServlet mapping in web.xml, often using the * .do, *. xhtml and other means. This determines the request URL must be a suffix of the URL, but not a true REST-style URL.

If the request is mapped DispatcherServlet configured as "/", the Spring MVC will capture all requests Web container, including requests for static resources, they will be treated as a normal Spring MVC request processing, and therefore can not find the corresponding processor will result in an error.

How to make Spring framework can capture all URL requests, but will also handle requests for static resources transferred by the Web container, is mapping configuration DispatcherServlet request is "/" can be provided. Because REST is one of the most important functions Spring3.0, so the team is serious about Spring static resources to handle this task, it gives a classic two solutions.

DispatcherServlet to adjust the configuration of the web.xml so that it can capture all requests:

Copy the code
<servlet>
        <servlet-name>springMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springMVC</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
Copy the code

 

By configuring the above url-pattern of all URL requests are intercepted DispatcherServlet of Spring MVC.

A method of using <mvc: default-servlet-handler />

<mvc:default-servlet-handler />

Disposed in springMVC-servlet.xml: after <mvc default-servlet-handler />, defines a org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler in Spring MVC context, it will be like an inspector, entering DispatcherServlet the URL screening, if found to be a static resource request, it forwards the request is handled by the default Web application server Servlet, if the request is not static resources before continuing to process the DispatcherServlet.

General Servlet Web application server default name is "default", thus DefaultServletHttpRequestHandler can find it. If the default Servlet names of all of your Web application server is not a "default", you need to display specified by default-servlet-name attribute:

<Mvc: default-servlet-handler default-servlet-name = "Servlet name of the Web server uses the default" />

2. The method of using <mvc: resources />

<Mvc: default-servlet-handler /> will be returned to deal with static resources Web application server process via the Spring MVC framework. The <mvc: resources /> Furthermore, by the Spring MVC framework to handle their own static resources, and add some useful value-added features.

First of all, <mvc: resources /> allows static resources placed anywhere, such as the WEB-INF directory, such as class path, you can even hit the JavaScript and other static files JAR package. Static resources specified location by the location attribute, since the location attribute is Resources type, can be used as "classpath:" prefix specifies resources such as resource location. Web container traditional static resources can only be placed in the root path of the Web container, <mvc: resources /> completely eliminates this restriction.

Secondly, <mvc: resources /> based on the current well-known Page Speed, YSlow and other browser optimization principle to provide optimization of static resources. You can specify static resources by cacheSeconds property at the time the browser cache, and can be set for one year the time to take advantage of the browser's cache. When the output of static resources, will be a good response packet header Expires and Cache-Control value according to configuration settings.

Upon receiving the static resource acquisition request, will check the request header Last-Modified value of static resource if there is no change, the process directly returns a corresponding status code 303, using the browser prompts the client data cache, rather than static resource the contents of the output to the client in order to fully save bandwidth and improve application performance.

Adding follows springMVC-servlet in:

<mvc:resources location="/,classpath:/META-INF/publicResources/" mapping="/resources/**"/>

 

Under the above configuration, Web root "/" class path and / META-INF / publicResources / mapped to the directory / resources path. Web images have under the assumption that the root path, these two js resource directory, in the images below bg.gif picture, there test.js js file below, you can /resources/images/bg.gif and / resources / js / test.js visit these two static resources.

Assume WebRoot also has images / bg1.gif and js / test1.js, it can also be referenced by /resources/images/bg1.gif and /resources/js/test1.js in a Web page.

Guess you like

Origin www.cnblogs.com/aligege/p/11620383.html