Sprinvmvc three ways to handle static resources

Source: Internet
Author: User

SPRINGMVC provides <mvc:resources> to set up static resources, but increasing this setting will still be intercepted by the interceptor if the use of wildcards increases the interceptor, which can be resolved using the following scheme:

Scenario one, the interceptor adds no filtering for static resources (involving Spring-mvc.xml)

 1 <mvc:resources location= "/" mapping= "/**/*.js"/> 2 <mvc:resources location= "/" mapping= "/**/*.css"/> 3 <mvc:resources location= "/assets/" mapping= "/assets/**/*"/> 4 <mvc:resources location= "/images/" mapping= "/  images/* "cache-period=" 360000 "/> 5 6 <mvc:interceptors> 7 <mvc:interceptor> 8 <mvc:mapping Path= "/**/*"/> 9 <mvc:exclude-mapping path= "/**/fonts/*"/>10 <mvc:exclude-mapping path= "/**/* . css "/>11 <mvc:exclude-mapping path="/**/*.js "/>12 <mvc:exclude-mapping path="/**/*.png "/>1         3 <mvc:exclude-mapping path= "/**/*.gif"/>14 <mvc:exclude-mapping path= "/**/*.jpg"/>15 <mvc:exclude-mapping path= "/**/*.jpeg"/>16 <mvc:exclude-mapping path= "/**/*login*"/>17 <mvc : exclude-mapping path= "/**/*login*"/>18 <bean class= "Com.luwei.console.mg.interceptor.VisitInterceptor" > </bean>19 &LT;/MVC:Interceptor>20 </mvc:interceptors> 

Scenario two, using the default static resource handling servlet to handle static resources (involving Spring-mvc.xml, Web. xml)

To enable the default servlet in Spring-mvc.xml

1 <mvc:default-servlet-handler/>

Increase the processing of static resources in Web. xml

1 <servlet-mapping>    2     <servlet-name>default</servlet-name>    3     <url-pattern >*.js</url-pattern>    4     <url-pattern>*.css</url-pattern>    5     <url-pattern >/assets/* "</url-pattern>    6     <url-pattern>/images/*</url-pattern>    

But the current setting must be in front of spring's dispatcher

Scenario three, modify spring's global interception setting to *.do interception (involving web. xml)

1 <servlet> 2     <servlet-name>SpringMVC</servlet-name> 3     <servlet-class> Org.springframework.web.servlet.dispatcherservlet</servlet-class> 4     <init-param> 5         < Param-name>contextconfiglocation</param-name> 6         <param-value>classpath:spring-mvc.xml</ Param-value> 7     </init-param> 8     <load-on-startup>1</load-on-startup> 9     < async-supported>true</async-supported>10 </servlet>11 <servlet-mapping>12     < servlet-name>springmvc</servlet-name>13     <url-pattern>*.do</url-pattern>14 </ Servlet-mapping>

With this setting, spring will only process requests that end with '. Do ' and no longer maintain static resources

For the pros and cons of these three scenarios:

The first scenario configuration is bloated, multiple interceptors increase the number of file lines, not recommended; the second scenario uses the default servlet for resource file access, spring intercepts all requests, and then the resource files are processed by default Sevlet, with few performance losses The third option, spring, is to deal with access that ends with '. Do ', which is more performance-efficient, but must end with '. Do ' on the re-access path, and the URL is less elegant;

In summary, it is recommended to use the second and third programs

Sprinvmvc three ways to handle static resources

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.