1, if you only configure the interception of URLs similar to *.do format, then access to the static resources is not a problem, as follows:
<!--&NBSP;SPRINGMVC Core distributor --> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</ servlet-class> <init-param> <param-name>contextconfiglocation</ Param-name> <param-value >classpath*:config/dispatcher-servlet.xml</param-value> </init-param> <load-on-startup>1 </load-on-startup> </servlet> <!-- Mapping *.do Request &NBSP;-->&NBSP;&NBSP;&NBsp; <servlet-mapping> <servlet-name> Dispatcherservlet</servlet-name> <url-pattern >*.do</url-pattern> </servlet-mapping>
Span style= "font-family: Song body; Font-size:small; line-height:19.5px; Background-color:rgb (255, 255, 255); " >2, if the configuration intercepts all requests, as follows:
<!--&NBSP;SPRINGMVC Core distributor --> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</ servlet-class> <init-param> <param-name>contextconfiglocation</ Param-name> <param-value >classpath*:config/dispatcher-servlet.xml</param-value> </init-param> <load-on-startup>1 </load-on-startup> </servlet> <!-- Map all requests --> &nbsP; <servlet-mapping> <servlet-name> Dispatcherservlet</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. Such a configuration, will cause JS files, CSS files, image files and other static resources can not be accessed.
Workaround:
1. 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"/>
2. 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/**"/>
can also be easily configured Web-i Nf/spring-servlet.xml as follows:
<?xml version= "1.0" encoding= "UTF-8"? ><beans:beans xmlns= "http// Www.springframework.org/schema/mvc " xmlns:xsi=" http// Www.w3.org/2001/XMLSchema-instance " xmlns:beans=" http// Www.springframework.org/schema/beans " xsi:schemalocation=" http://www.springframework.org/schema/mvc http:// www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http:// Www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd " > <!-- dispatcherservlet context: defines this servlet ' s request-processing infrastructure --> <!-- Enables the Spring MVC @Controller programming model --> <annotation-driven /> <!--&NBSP;SPRING&NBSP;MVC access to static resources --> <resources location= "/image/" mapping= "/image/**"/> <resources location= "/js/" mapping= "/ js/** "/> <resources location= "/style/" mapping= "/style/**"/> </beans:beans>
SPRINGMVC Accessing static resources