The role of <mvc:default-servlet-handler/>

 

 

The resource URL of the elegant REST style does not want to have suffixes such as .html or .do. Since the early Spring MVC cannot handle static resources well, the request mapping of DispatcherServlet is configured in web.xml, often using *.do, *. xhtml etc. This determines that the request URL must be a suffixed URL, and cannot be a true REST-style URL.

If the DispatcherServlet request mapping is configured as "/", Spring MVC will capture all requests from the web container, including requests for static resources, and Spring MVC will treat them as a normal request, so it will cause an error if the corresponding handler cannot be found.

How to enable the Spring framework to capture all URL requests and transfer requests for static resources to the Web container for processing is the premise that the request mapping of DispatcherServlet can be configured as "/". Since REST is one of the most important functions of Spring 3.0, the Spring team attaches great importance to the task of static resource processing and provides two classic solutions.

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

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

 

Through the configuration of url-pattern above, all URL requests will be intercepted by Spring MVC's DispatcherServlet.

Method 1. Use <mvc:default-servlet-handler />

<mvc:default-servlet-handler />

After configuring <mvc:default-servlet-handler /> in springMVC-servlet.xml, a org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler will be defined in the Spring MVC context, which will act like an inspector for entering DispatcherServlet If it is found to be a request for static resources, the request will be transferred to the default Servlet of the Web application server for processing. If it is not a request of static resources, the DispatcherServlet will continue to process it.

The default servlet name of a typical web application server is "default", so DefaultServletHttpRequestHandler can find it. If the default servlet name for all your web application servers is not "default", you need to explicitly specify it via the default-servlet-name attribute:

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

Method 2. Using <mvc:resources />

<mvc:default-servlet-handler />将静态资源的处理经由Spring MVC框架交回Web应用服务器处理。而<mvc:resources />更进一步,由Spring MVC框架自己处理静态资源,并添加一些有用的附加值功能。

首先,<mvc:resources />允许静态资源放在任何地方,如WEB-INF目录下、类路径下等,你甚至可以将JavaScript等静态文件打到JAR包中。通过location属性指定静态资源的位置,由于location属性是Resources类型,因此可以使用诸如"classpath:"等的资源前缀指定资源位置。传统Web容器的静态资源只能放在Web容器的根路径下,<mvc:resources />完全打破了这个限制。

其次,<mvc:resources />依据当前著名的Page Speed、YSlow等浏览器优化原则对静态资源提供优化。你可以通过cacheSeconds属性指定静态资源在浏览器端的缓存时间,一般可将该时间设置为一年,以充分利用浏览器端的缓存。在输出静态资源时,会根据配置设置好响应报文头的Expires 和 Cache-Control值。

在接收到静态资源的获取请求时,会检查请求头的Last-Modified值,如果静态资源没有发生变化,则直接返回303相应状态码,提示客户端使用浏览器缓存的数据,而非将静态资源的内容输出到客户端,以充分节省带宽,提高程序性能。

在springMVC-servlet中添加如下配置:

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

 

The above configuration maps the Web root path "/" and the directory /META-INF/publicResources/ under the classpath to the /resources path. Assuming that there are two resource directories, images and js, under the Web root path, bg.gif image under images, and test.js file under js, you can pass /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.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326373085&siteId=291194637