如果我们使用spring mvc来做web访问请求的控制转发,那么默认所有访问都将被DispatcherServlet独裁统治。比如我现在想访问的欢迎页index.html根本无需任何业务逻辑处理,仅仅就展示一句话而已,但spring mvc还是会把访问index.html这件事交给DispatcherServlet处理,而DispatcherServlet会从HandlerMapping去找对应Controller注解,如果找不到就报错了:

No mapping found for HTTP request with URI [/index.html] in DispatcherServlet with name 'service-dispatcher'

  spring mvc提供的解决方案是在配置文件中加入mvc:resources来专门标识静态资源,比如我的web.xml是这样的:

<!DOCTYPE web-app PUBLIC"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN""http://java.sun.com/dtd/web-app_2_3.dtd" ><web-app><display-name>Memcache View Application</display-name><servlet><servlet-name>service-dispatcher</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring-mvc.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>service-dispatcher</servlet-name><url-pattern>/</url-pattern></servlet-mapping><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring-core.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><welcome-file-list><welcome-file>index.html</welcome-file></welcome-file-list>
</web-app>

  那么我的spring-mvc.xml就要是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beans     http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "><context:component-scan base-package="com.wulinfeng.memcache.view" /><mvc:annotation-driven /><mvc:resources location="/" mapping="/**/*.js"/>  <mvc:resources location="/" mapping="/**/*.css"/>  <mvc:resources location="/" mapping="/**/*.png"/><mvc:resources location="/" mapping="/*.html"/>
</beans>

  这时就可以顺利访问index.html这个欢迎页了。其他js、css或png图片的静态资源也可以直接访问。如果我还要做拦截,比如登陆功能,又不想拦截到静态资源,怎么办呢?最简单的就是配置拦截时过滤静态资源:

    <mvc:interceptors><mvc:interceptor><mvc:mapping path="/**" /><mvc:exclude-mapping path="/**/*.css" /><mvc:exclude-mapping path="/**/*.js" /><mvc:exclude-mapping path="/**/*.png" /><mvc:exclude-mapping path="*.html" /><mvc:exclude-mapping path="/login**" /><mvc:exclude-mapping path="/register**" /><mvc:exclude-mapping path="/getVerifyCode**" /><mvc:exclude-mapping path="/getMethod/**" /><bean class="com.wulinfeng.test.testpilling.util.InterceptorUtil" /></mvc:interceptor></mvc:interceptors>

转载于:https://www.cnblogs.com/wuxun1997/p/7884733.html

spring mvc静态资源访问的配置相关推荐

  1. Spring Boot静态资源访问和配置全解析

    在web开发中,静态资源的访问时必不可少的,比如image.css.js等.SpringBoot对静态资源访问提供了很好的支持,使用其提供的基本默认配置基本可以满足开发需求,同时,又支持开发人员进行自 ...

  2. Spring Boot 静态资源访问原理解析

    一.前言 springboot配置静态资源方式是多种多样,接下来我会介绍其中几种方式,并解析一下其中的原理. 二.使用properties属性进行配置 应该说 spring.mvc.static-pa ...

  3. Spring Boot集成Ueditor富文本编辑器,实现图片上传,视频上传,返回内容功能并且通过OSS转换为链接并且解决Spring Security静态资源访问以及跨域问题

    学习自https://cloud.tencent.com/developer/article/1452451 现在是晚上22点,刚刚和我们的前端交流完了富文本编辑器的一些意见和看法 还是老样子 需求 ...

  4. Spring MVC静态资源处理(转)

    优雅REST风格的资源URL不希望带 .html 或 .do 等后缀.由于早期的Spring MVC不能很好地处理静态资源,所以在web.xml中配置DispatcherServlet的请求映射,往往 ...

  5. Spring MVC静态资源处理

    优雅REST风格的资源URL不希望带 .html 或 .do 等后缀.由于早期的Spring MVC不能很好地处理静态资源,所以在web.xml中配置DispatcherServlet的请求映射,往往 ...

  6. Spring MVC静态资源实例

    以下内容引用自http://wiki.jikexueyuan.com/project/spring/mvc-framework/spring-static-pages-example.html: 例子 ...

  7. SpringMVC REST 风格静态资源访问配置

    1 在web.xml中使用默认servlet处理静态资源,缺点是如果静态资源过多,则配置量会比较大,一旦有遗漏,则会造成资源无法正常显示或404错误. <!-- 静态资源访问控制 -->& ...

  8. spring boot 整合web开发之文件上传、静态资源访问、异常处理、返回JSON数据

    目录 springboot 整合web开发 返回json数据 静态资源访问 文件上传 全局异常 1.返回json数据 springboot默认的是jackson-databind做为json处理器.也 ...

  9. web静态资源访问规则||webjars的访问配置——webjars是maven库里面对css js image打的一个jar包

    Html css js image  txt   web项目中 放在 Webapp 在springboot项目中  静态资源放置的位置 Springboot默认的静态资源目录 (1)在src/main ...

最新文章

  1. shell 函数返回值接收问题
  2. python真的那么强大嘛-这些 Python 库真的很“冷”,但是却很强大
  3. row间距 table 某一行_UITableview的一个section下的各行Row之间可以设置间隔一段距离吗?...
  4. python基础面试题整理---从零开始 每天十题(04)
  5. python with循环_Python for循环、while循环
  6. html加上百度统计,vue单页面应用加入百度统计
  7. MyBatis接口代理
  8. 欧几里得和扩展欧几里得
  9. html九宫格实现人像拼图游戏,实例分享jQuery+vue.js实现的九宫格拼图游戏
  10. IP库GeoLite2-City.mmdb的使用
  11. 太阳光轨迹软件_巧用虚拟天文馆软件Stellarium演示太阳周日视运动轨迹_贺志康...
  12. python将网页转换为图片保存
  13. 导入以及导入模板下载
  14. 图解:卷积神经网络数学原理
  15. oracle查询倒叙,ORACLE 索引结构--倒叙索引:
  16. Java双重检查懒汉式单例模式中volatile的作用
  17. pointwise 18.4R3 cfd前处理网格生成软件
  18. 交换机工作原理,收到一个数据包后交换机是如何处理的(实验加抓包详细了解)
  19. cocos2d-x 使用位图工具BMFont自定义字体 fnt
  20. 怎样制作微信小程序?

热门文章

  1. 从零开始-小程序采坑记录
  2. 微服务之迷思--转几位大牛的文章
  3. C#读取资源文件的两种方法及保存资源文件到本地
  4. js实现浏览器后退页面刷新
  5. 修改密码导致应用程序池无法启动
  6. Erlang中使用变量的简单示例
  7. 使用C# lock同时访问共享数据
  8. Java笔试题库之选题题篇【141-210题】
  9. 小李飞刀:SQL题目第二弹!
  10. vsphere入门之高可用性和双机热备