SpringMVC提供<mvc:resources>來(lái)設(shè)置靜態(tài)資源,但是增加該設(shè)置如果采用通配符的方式增加攔截器的話(huà)仍然會(huì)被攔截器攔截,可采用如下方案進(jìn)行解決:
方案一、攔截器中增加針對(duì)靜態(tài)資源不進(jìn)行過(guò)濾(涉及spring-mvc.xml)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
< 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
1
|
< mvc:default-servlet-handler /> |
在web.xml中增加對(duì)靜態(tài)資源的處理
1
2
3
4
5
6
7
|
< 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)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
< 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 > |
這樣設(shè)置,Spring就會(huì)只針對(duì)以'.do'結(jié)尾的請(qǐng)求進(jìn)行處理,不再維護(hù)靜態(tài)資源
針對(duì)這三種方案的優(yōu)劣分析:
第一種方案配置比較臃腫,多個(gè)攔截器時(shí)增加文件行數(shù),不推薦使用;
第二種方案使用默認(rèn)的Servlet進(jìn)行資源文件的訪問(wèn),Spring攔截所有請(qǐng)求,然后再將資源文件交由默認(rèn)的Sevlet進(jìn)行處理,性能上少有損耗;
第三種方案Spring只是處理以'.action'結(jié)尾的訪問(wèn),性能上更加高效,但是再訪問(wèn)路徑上必須都以'.action'結(jié)尾,URL不太文雅;
綜上所述,推薦使用第二和第三中方案
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://www.cnblogs.com/wyb628/p/6813985.html