日期:2014-05-16  浏览次数:20489 次

Spring3.0 做rest时候拦截js、图片
web.xml配置如下:
<servlet>  
    <servlet-name>springmvc</servlet-name>  
    <servlet-class> org.springframework.web.servlet.DispatcherServlet  
    </servlet-class>  
    <init-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>/WEB-INF/spring-mvc.xml</param-value>  
    </init-param>  
    <load-on-startup>2</load-on-startup>  
</servlet>  
<servlet-mapping>  
    <servlet-name>springmvc</servlet-name>  
    <url-pattern>/</url-pattern>  
</servlet-mapping> 

<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
?

这时候,spring回拦截所有的请求,包括css,js,导致找不到css。js。

解决办法:

在web.xml中加入下面配置:

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

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

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

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

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

?

1 楼 kevinhrw 2012-04-11  
一个好办法
不知道使用"mvc:resources"应该怎么写?