SpringMVC 攔截器(interceptors)對樣式(css),JavaScript(js),圖片(images)鏈接的攔截


因為在web.xml配置了

<servlet-mapping>
		<servlet-name>appServlet</servlet-name>
		<url-pattern>/</url-pattern>
</servlet-mapping>

導致對所有連接都會經DispatcherServlet,所以靜態資源如css,js,images都會被過濾到,從而導致頁面沒法渲染成功。

不過,我們可以在主配置文件中,添加<mvc:resources location="">,從而能夠使得靜態資源不會經過DispatcherServlet,就可以成功渲染頁面了。

      <!-- 處理靜態資源的請求 -->
	<mvc:resources location="/WEB-INF/views/css/" mapping="/css/**" />
	<mvc:resources location="/WEB-INF/views/js/" mapping="/js/**" />
	<mvc:resources location="/images/" mapping="/images/**" />

然而,SpringMVC還有攔截器的機制(如果你沒用攔截器,那么就不會有問題),反而就把我們靜態資源的請求鏈接也給攔截了,

通過我在攔截器里輸出看到了,確實會把靜態資源的請求鏈接也攔截到了,所以我頁面就會產生如下錯誤:

Resource interpreted as Stylesheet but transferred with MIME type text/html: 

我還是沒找到其他原因,所以就在攔截器上把靜態資源的鏈接給過濾了,然后就沒產生上面的問題了。

雖然在其他頁面倒是沒有產生上面的問題,不過把靜態資源的鏈接過濾了,應該也不會產生什么影響。

<mvc:interceptors>
		<mvc:interceptor>
			<mvc:mapping path="/**"/> <!-- 因為我對所有鏈接都攔截,所以靜態資源的鏈接也被攔截了 -->
			<mvc:exclude-mapping path="/js/**"/>
			<mvc:exclude-mapping path="/css/**"/>
			<mvc:exclude-mapping path="/images/**"/>
			<bean class="com.databasegroup.interceptor.AuthInterceptor"></bean>
		</mvc:interceptor>
</mvc:interceptors>

  

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM