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

來源:互聯網
上載者:User

標籤:script   渲染   串連   resources   處理   ret   images   style   html   

因為在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>

  

  

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.