Spring MVC static Resource Processing

Source: Internet
Author: User

The elegant rest-style resource URL does not want to have a. html or. Do suffix. Since the early spring MVC does not handle static resources well, the request mappings for Dispatcherservlet are configured in Web. XML, often using *.do, *. XHTML, and so on. This determines that the request URL must be a URL with a suffix, rather than a true restful URL.

If the Dispatcherservlet request mapping is configured as "/", spring MVC captures all requests from the Web container, including requests for static resources, and spring MVC treats them as a normal request, so finding the corresponding processor will result in an error.

How to enable the spring framework to capture requests for all URLs while simultaneously forwarding requests for static resources to the Web container is a precondition for configuring Dispatcherservlet request mappings as "/". Because rest is one of the most important features of Spring3.0, the spring team values the task of static resource handling, giving two classic solutions.

Adjust the configuration of the Dispatcherservlet in Web. XML so that it can capture all requests:

    <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>

With the above Url-pattern configuration, all URL requests will be intercepted by spring MVC's dispatcherservlet.

Using <mvc:default-servlet-handler/>

After configuring <mvc:default-servlet-handler/> in Springmvc-servlet.xml, the spring The MVC context defines a org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler, which, like an inspector, screens the URL into the Dispatcherservlet If a request is found to be a static resource, the request is forwarded to the Web application server's default servlet processing, which is processed by Dispatcherservlet if it is not a request for a static resource.

General Web application Server The default servlet name is "default", so Defaultservlethttprequesthandler can find it. If your default servlet name for all Web application servers is not "default", you need to display the specified through the Default-servlet-name property:

<mvc:default-servlet-handler default-servlet-name= "The Web server used by default is used by the servlet name"/>

Using <mvc:resources/>

<mvc:default-servlet-handler/> handles the processing of static resources back to the Web application server through the Spring MVC framework. and <mvc:resources/> Further, the Spring MVC framework handles static resources by itself and adds some useful added value features.

First, <mvc:resources/> allows static resources to be placed anywhere, such as the Web-inf directory, under the Classpath, and you can even hit a static file such as JavaScript into the jar package. The Location property specifies the position of the static resource, because the Site property is a resource type, so you can specify the resource location by using a resources prefix such as "classpath:". The static resources of a traditional web container can only be placed under the root path of the Web container, and <mvc:resources/> completely breaks the limit.

Second, <mvc:resources/> optimizes static resources based on the current well-known browser optimization principles of page speed and YSlow. You can use the Cacheseconds property to specify the cache time for a static resource on the browser side, which can typically be set to one year to take advantage of browser-side caching. When you output a static resource, the expires and Cache-control values of the message headers are configured to respond to the configuration.

When a request for a static resource is received, the last-modified value of the request header is checked, and if the static resource does not change, the corresponding status code is returned directly, prompting the client to use the data cached by the browser rather than outputting the contents of the static resource to the client to fully conserve bandwidth. Improve program performance.

In Springmvc-servlet, add the following configuration:

<mvc:resources location= "/,classpath:/meta-inf/publicresources/" mapping= "/resources/**"/>

The above configuration maps the/meta-inf/publicresources/directory under the Web root path "/" and the Classpath to the/resources path. Suppose the Web root path has images, JS these two resources directory, under images there are bg.gif pictures, under JS test.js file, you can/resources/images/bg.gif and/resources/js/ Test.js accesses these two static resources.

Assuming that Webroot also has images/bg1.gif and js/test1.js, it can also be referenced in Web pages through/resources/images/bg1.gif and/resources/js/test1.js.

Transfer from http://www.cnblogs.com/fangqi/archive/2012/10/28/2743108.html

Spring MVC static Resource Processing

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.