在开发过程中遇到SpringMVC把JS等资源拦截的问题,于是配置web.xml如下:
<servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> <!-- 避免静态文件被阻截 --> <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-name>default</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping>
然后出现了无法找到controller的404错误,后来配置如下:
<servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <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-name>default</servlet-name> <url-pattern>*.html</url-pattern> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>问题解决,资源白名单应放在dispatchche的前面
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>