`

(转)怎么让静态资源不被SpringMVC分配器过滤?

 
阅读更多
问题是这样的:
在SpringMVC项目中,如果web.xml中配置为这样:
   
<servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>
                       org.springframework.web.servlet.DispatcherServlet
                </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

那静态资源,如js文件、css文件、图片等,都会经过org.springframework.web.servlet.DispatcherServlet过滤,DispatcherServlet当然处理不了这些文件,所以这些文件就发送不到客户端了。

SpringMVC从3.0.4版本开始,新增了一种配置可以解决这个问题,具体配置如下:


   
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/beans   
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.2.xsd">

    <context:component-scan base-package="com.XXX.XXX" />

    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/pages/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
    
    <mvc:resources location="/r/" mapping="/r/**" />
    <mvc:annotation-driven />
</beans>



新增的是<mvc:resources location="/r/" mapping="/r/**" />这个配置,相当于告诉SpringMVC,凡是请求路径为 /r/ 开始的,都自动映射到 r目录 下面相同文件名的文件去,而不经过DispatcherServlet过滤,这样就已经搞定了。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics