spring mvc 加载静态文件

使用spring mvc时,在web.xml中配置
<servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
</servlet-mapping>
这样配置方便,但有个问题,就是在jsp文件中引入js,css,img文件时会作为一个请求,而controller中没有处理这样请求的方法,最终导致引入失败,使用firefox查看请求,所有引用的资源都是404.
解决方式:
1.<mvc:resources location="/js/" mapping="/js/**"/>
        <mvc:resources location="/img/" mapping="/img/**"/>
2.<mvc:default-servlet-handler/>

注:配置文件中的mvc标签可能无法使用,可能没有引入spring-mvc-3.0-xsd。如果确定已引入了,请检查spring版本,必须是3.0以上版本,3.0也不可以。

猜你喜欢

转载自447214075.iteye.com/blog/1950258