SpringMVC 攔截器不攔截靜態(tài)資源的三種處理方式方法
SpringMVC提供<mvc:resources>來設(shè)置靜態(tài)資源,但是增加該設(shè)置如果采用通配符的方式增加攔截器的話仍然會被攔截器攔截,可采用如下方案進(jìn)行解決:
方案一、攔截器中增加針對靜態(tài)資源不進(jìn)行過濾(涉及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-mapping path="/**/*.gif"/>
<mvc:exclude-mapping path="/**/*.jpg"/>
<mvc:exclude-mapping path="/**/*.jpeg"/>
<mvc:exclude-mapping path="/**/*login*"/>
<mvc:exclude-mapping path="/**/*Login*"/>
<bean class="com.luwei.console.mg.interceptor.VisitInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
方案二、使用默認(rèn)的靜態(tài)資源處理Servlet處理靜態(tài)資源(涉及spring-mvc.xml, web.xml)
在spring-mvc.xml中啟用默認(rèn)Servlet
<mvc:default-servlet-handler/>
在web.xml中增加對靜態(tài)資源的處理
<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>
但是當(dāng)前的設(shè)置必須在Spring的Dispatcher的前面
方案三、修改Spring的全局?jǐn)r截設(shè)置為*.do的攔截(涉及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>*.do</url-pattern>
</servlet-mapping>
這樣設(shè)置,Spring就會只針對以'.do'結(jié)尾的請求進(jìn)行處理,不再維護靜態(tài)資源
針對這三種方案的優(yōu)劣分析:
第一種方案配置比較臃腫,多個攔截器時增加文件行數(shù),不推薦使用;第二種方案使用默認(rèn)的Servlet進(jìn)行資源文件的訪問,Spring攔截所有請求,然后再將資源文件交由默認(rèn)的Sevlet進(jìn)行處理,性能上少有損耗;第三種方案Spring只是處理以'.do'結(jié)尾的訪問,性能上更加高效,但是再訪問路徑上必須都以'.do'結(jié)尾,URL不太文雅;
綜上所述,推薦使用第二和第三中方案
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java源碼解析阻塞隊列ArrayBlockingQueue常用方法
今天小編就為大家分享一篇關(guān)于Java源碼解析阻塞隊列ArrayBlockingQueue常用方法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-01-01
java使用Hex編碼解碼實現(xiàn)Aes加密解密功能示例
這篇文章主要介紹了java使用Hex編碼解碼實現(xiàn)Aes加密解密功能,結(jié)合完整實例形式分析了Aes加密解密功能的定義與使用方法,需要的朋友可以參考下2017-01-01
關(guān)于JSqlparser使用攻略(高效的SQL解析工具)
這篇文章主要介紹了關(guān)于JSqlparser使用攻略(高效的SQL解析工具),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11
Springboot之@Controller注解不生效問題及解決
這篇文章主要介紹了Springboot之@Controller注解不生效問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10

