spring mvc - 對靜態資源的處理

spring mvc - 對靜態資源的處理css

spring對靜態資源文件(js css htm html jpg jepg png bmp)等的處理方式以下:html

1、交由上級web容器處理,在web.xml中的org.springframework.web.servlet.DispatcherServlet 配置以前加入以下代碼:java

<servlet-mapping>   
    <servlet-name>default</servlet-name>   
    <url-pattern>*.css</url-pattern>   
</servlet-mapping>

根據不一樣的資源文件制定不一樣的映射機制,其中servlet-name在tomcat中默認是default,該servlet在tomcat\config\web.xml文件中默認配置,不一樣的應用服務器對應的靜態資源文件處理的servlet名稱不盡相同,以下:web

tomcat,jboss,glassfish,jetty : defaut;  
weblogic : FileServlet;  
websphere : SimpleFileServlet;  
resin : resin-file;  
GAE : _ah_default

2、交由spring mvc:resources 處理
在dispatchServlet.xml文件中增長
<mvc:resources location="/WEB-INF/resource/" mapping="/resource/**"/>
該方式雖然靜態資源文件能夠訪問了,可是若是系統配置了通配符的攔截器,靜態資源仍是會被攔截器攔截,以下:spring

<mvc:interceptors>  
    <mvc:interceptor>  
        <mvc:mapping path="/*"/>   
    <bean  class="com.xinlong.cms.front.interceptor.CmsFrontInterceptor"></bean>  
    </mvc:interceptor>  
</mvc:interceptors>

所以須要修改,以下:spring-mvc

<mvc:interceptors>  
    <mvc:interceptor>  
        <mvc:mapping path="/*"/>   
        <mvc:exclude-mapping path="/resource/**"/>  
    <bean  class="com.xinlong.cms.front.interceptor.CmsFrontInterceptor"></bean>  
    </mvc:interceptor>  
</mvc:interceptors>

到此還有個問題mvc:exclude-mapping 標籤不被spring-mvc-3.0.xsd支持,該配置在spring-mvc-3.2.xsd中,能夠經過http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd獲取,所以須要在spring-mvc-3.0.xsd文件中增長該標籤,處理辦法很簡單,從網上下載spring-mvc-3.2.xsd,找到標籤後,再找到spring.web.servlet-3.0.5.jar中的spring-mvc-3.0.xsd文件,將標籤加入便可tomcat

相關文章
相關標籤/搜索