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