Common problems in JSP to prevent SPRINGMVC interceptors from blocking JS and other static resource files solution

Source: Internet
Author: User

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

<mvc:resources location= "/" mapping= "/**/*.js"/> <mvc:resources location= "/" mapping= "/**/*.css"/> < Mvc:resources location= "/assets/" mapping= "/assets/**/*"/> <mvc:resources location= "/images/" mapping= "/ images/* "cache-period=" 360000 "/><mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/**/*        "/> <mvc:exclude-mapping path="/**/fonts/* "/> <mvc:exclude-mapping path="/**/*.css "/> <mvc:exclude-mapping path= "/**/*.js"/> <mvc:exclude-mapping path= "/**/*.png"/> <mvc:exclude-m Apping path= "/**/*.gif"/> <mvc:exclude-mapping path= "/**/*.jpg"/> <mvc:exclude-mapping path= "/** /*.jpeg "/> <mvc:exclude-mapping path="/**/*login* "/> <mvc:exclude-mapping path="/**/*login* "/&gt        ; <bean class= "Com.luwei.console.mg.interceptor.VisitInterceptor" ></bean> </mvc:interceptor>< /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

<servlet-mapping>        <servlet-name>default</servlet-name>        <url-pattern>*.js</ url-pattern>        <url-pattern>*.css</url-pattern>        <url-pattern>/assets/* "</ url-pattern>        <url-pattern>/images/*</url-pattern>    </servlet-mapping>

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

<servlet>    <servlet-name>SpringMVC</servlet-name>    <servlet-class> org.springframework.web.servlet.dispatcherservlet</servlet-class>    <init-param>        < Param-name>contextconfiglocation</param-name>        <param-value>classpath:spring-mvc.xml</ param-value>    </init-param>    <load-on-startup>1</load-on-startup>    < Async-supported>true</async-supported></servlet><servlet-mapping>    <servlet-name >SpringMVC</servlet-name>    <url-pattern>*.action</url-pattern></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 more 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 sevlet the resource files to the default, with few performance losses;

The third option, spring, is to deal with access at the end of '. Action ', which is more performance-efficient, but must end with '. Action ' on the re-access path, and the URL is less elegant;

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

recommended for use after SpringMVC3.0:

<mvc:resources location= "/web-inf/html/" mapping= "/**/*.html"/><mvc:resources location= "/WEB-INF/html/" mapping= "/**/*.ico"/><mvc:resources location= "/web-inf/html/" mapping= "/**/*.js"/><mvc:resources location= "/web-inf/html/" mapping= "/**/*.css"/><mvc:resources location= "/web-inf/html/" mapping= "/**/*.png" /><mvc:resources location= "/web-inf/html/" mapping= "/**/*.gif"/><mvc:resources location= "/WEB-INF/ html/"mapping="/**/*.jpg "/><mvc:resources location="/web-inf/html/"mapping="/**/*.ttf "/><MVC: Resources location= "/web-inf/html/" mapping= "/**/*.woff"/><mvc:resources location= "/web-inf/html/" mapping= " /**/*.woff2 "/>

  

Common problems in JSP to prevent SPRINGMVC interceptors from blocking the solution of static resource files such as JS

Related Article

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.