防止SpringMVC攔截器攔截js等靜態(tài)資源文件的解決方法
SpringMVC提供<mvc:resources>來(lái)設(shè)置靜態(tài)資源,但是增加該設(shè)置如果采用通配符的方式增加攔截器的話仍然會(huì)被攔截器攔截,可采用如下方案進(jìn)行解決:
方案一、攔截器中增加針對(duì)靜態(tài)資源不進(jìn)行過(guò)濾(涉及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中增加對(duì)靜態(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>*.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í)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java IO復(fù)用_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了Java IO復(fù)用的相關(guān)知識(shí),非常不錯(cuò),具有參考借鑒價(jià)值,需要的的朋友參考下吧2017-05-05
利用java實(shí)現(xiàn)中獎(jiǎng)概率詳情
這篇文章主要介紹了利用java實(shí)現(xiàn)中獎(jiǎng)概率詳情,根據(jù)概率將獎(jiǎng)品劃分區(qū)間,每個(gè)區(qū)間代表一個(gè)獎(jiǎng)品,然后抽取???隨機(jī)數(shù)??,反查落在那個(gè)區(qū)間上,即為所抽取的獎(jiǎng)品,需要的朋友可以參考一下2022-07-07
詳解json string轉(zhuǎn)換為java bean及實(shí)例代碼
這篇文章主要介紹了詳解json string轉(zhuǎn)換為java bean及實(shí)例代碼的相關(guān)資料,這里提供實(shí)例代碼幫助大家理解,需要的朋友可以參考下2017-07-07
Java實(shí)現(xiàn)List去重的五種方法詳解
這篇文章主要為大家詳細(xì)介紹了Java中List去重的5種方法,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)和參考價(jià)值,需要的小伙伴可以了解一下2022-10-10
Eclipse?IDE中如何設(shè)置JVM啟動(dòng)參數(shù)
這篇文章主要介紹了Eclipse?IDE中如何設(shè)置JVM啟動(dòng)參數(shù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06

