九、静态资源访问

图片资源不能访问

  • 图片等资源不能访问
<img src="${pageContext.request.contextPath}/photo/1.png" alt="">
  • 被拦截包含png
<!-- 拦截所有,不包括jsp,包含.js .png.css (建议使用) -->
<url-pattern>/</url-pattern>

解决方案一(简单)

  • springmvc.xml根据url放行静态资源
<mvc:default-servlet-handler/>

解决方案二(性能快,开发推荐)

  • 采用spring自带的<mvc:resources>
<mvc:annotation-driven />
<mvc:resources location="/img/" mapping="/img/**"/>   
<mvc:resources location="/js/" mapping="/js/**"/>    
<mvc:resources location="/css/" mapping="/css/**"/>  
  • 说明/static为示例目录(springmvc.xml)
(1) location表示webapp目录下的static包下的所有文件;
(2) mapping表示以/static开头的所有请求路径,如/static/a 或者/static/a/b;
(3) 该配置的作用是:DispatcherServlet不会拦截以/static开头的所有请求路径,并当作静态资源交由Servlet处理

猜你喜欢

转载自blog.csdn.net/weixin_34343308/article/details/87685100