springmvc 请求经过controller后静态资源无法访问的问题

时间:2023-12-31 22:39:44

经过RequestMapping(“xx”)后 转发请求时会在url里面附带地址,

导致访问静态资源文件失败,

解决办法是在 spring-mvc.xml文件中加上

<mvc:default-servlet-handler/>
<!-- 由于在web.xml中定义的url拦截形式为“/”表示拦截所有的url请求,
包括静态资源例如css、js等。所以需要在springmvc.xml中添加资源映射标 -->
<mvc:resources location="/static/js/" mapping="/*/static/js/**"/>
<mvc:resources location="/static/css/" mapping="/*/static/css/**"/>
<mvc:resources location="/static/fonts/" mapping="/*/static/fonts/**"/>
<mvc:resources location="/static/images/" mapping="/*/static/images/**"/>
<mvc:resources location="/static/lib/" mapping="/*/static/lib/**"/> 以及在web.xml里加上
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/static/*</url-pattern>
</servlet-mapping>