SpringBoot(六)-- 静态资源处理
1.Spring Boot 的默认资源映射
其中默认配置的 /** 映射到 /static (或/public、/resources、/META-INF/resources),
其中默认配置的 /webjars/** 映射到 classpath:/META-INF/resources/webjars/,
上面的 static、public、resources 等目录都在 classpath: 下面(如 src/main/resources/static)。
2.自定义资源映射(推荐)
继承 WebMvcConfigurerAdapter 并重写方法 addResourceHandlers,例如:
registry.addResourceHandler("/image/**").addResourceLocations("file:H:/image/");
registry.addResourceHandler(“/image1/**”).addResourceLocations(“classpath:/img1/”)
代码:见此处
在static文件夹中加入 football.jpg 图片,在 resources文件夹中 新建 image文件夹,然后将 timg.jpg 图片放进去。
在访问浏览器中 访问 http://127.0.0.1:8080/image/football.jpg,出现图片,正常!
在访问浏览器中 访问 http://127.0.0.1:8080/timg.jpg,出现图片
3.通过配置文件映射(不推荐),application.properties
使用 spring.mvc.static-path-pattern 可以重新定义pattern,如修改为 /image/**,
使用 spring.resources.static-locations 可以重新定义 pattern 所指向的路径,支持 classpath: 和 file: 注意 spring.mvc.static-path-pattern 只可以定义一个,目前不支持多个逗号分割的方式。
例如:
# 默认值为 /**
spring.mvc.static-path-pattern= /image/**
# 默认值为 classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/
spring.resources.static-locations=classpath:/image/
4.源码下载
https://gitee.com/xbq168/spring-boot-learn
SpringBoot(六)-- 静态资源处理的更多相关文章
- SpringBoot 配置静态资源映射
SpringBoot 配置静态资源映射 (嵌入式servlet容器)先决知识 request.getSession().getServletContext().getRealPath("/& ...
- springboot设置静态资源不拦截的方法
springboot设置静态资源不拦截的方法 springboot不拦截静态资源需配置如下的类: import org.springframework.context.annotation.Confi ...
- springboot下静态资源的处理(转)
在SpringBoot中有默认的静态资源文件相关配置,需要通过如下源码跟踪: WebMvcAutoConfiguration-->configureResourceChain(method)-- ...
- IntelliJ IDEA+SpringBoot中静态资源访问路径陷阱:静态资源访问404
IntelliJ IDEA+SpringBoot中静态资源访问路径陷阱:静态资源访问404 .embody{ padding:10px 10px 10px; margin:0 -20px; borde ...
- springboot配置静态资源访问路径
其实在springboot中静态资源的映射文件是在resources目录下的static文件夹,springboot推荐我们将静态资源放在static文件夹下,因为默认配置就是classpath:/s ...
- 【SpringBoot】06.SpringBoot访问静态资源
SpringBoot访问静态资源 1.SpringBoot从classpath/static的目录 目录名称必须是static 启动项目,访问http://localhost:8080/0101.jp ...
- SpringBoot - 搭建静态资源存储服务器
目录 前言 环境 实现效果 具体实现 文件上传 配置类 上传接口 上传实现 辅助类 实体 上传测试 文件访问 配置类 项目源码 前言 记录下SpringBoot下静态资源存储服务器的搭建. 环境 wi ...
- springboot访问静态资源404
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring- ...
- SpringBoot之静态资源放行
为了提高开发效率,编写对应的代码生成器.代码生成器主要有两个方面,一个是在线Web,另外一个是运行某个类. 使用的技术是SpringBoot+MyBatis-Plus+MySQL+JDK8. 在编写在 ...
- spring-boot配置静态资源映射的坑:properties文件不能添加注释
如此博文所述,Spring Boot 对静态资源映射提供了默认配置 默认将 /** 所有访问映射到以下目录:classpath:/staticclasspath:/publicclasspath:/r ...
随机推荐
- R中ifelse、which、%in%的用法
R中ifelse.which.%in%的用法 (2014-02-08 13:54:08)标签: 教育 在R学习过程中,遇到了ifelse.which.%in%,下面分别举例,说明他们的用法.1.ife ...
- JavaScript 作用域链解析
JavaScript 中有 Scope( 作用域 ) , Scope chain( 作用域链 ) , Execute context( 执行上下文 ) , Active Object ( 活动对象 ) ...
- Controllerizing the ScrollViewer Thumbnail
In the last post we created a ScrollViewer Thumbnail feature using a just a bit of Xaml and databind ...
- mysql 删除
DROP删表,表结构将删了,当然数据也不存在了 TRUNCATE和DELETE删数据,表结构还在 DELETE可以带条件删除,TRUNCATE是全部删除 DELETE删除会写日志,TRUNCATE不写 ...
- JS对checkbox全选和取消全选
需求:checkbox控制列表数据全选与取消全选择. 效果图: 1.html <body > <input type="button" name="in ...
- vb.net与c#相互转换工具
vb.net与c#相互转换工具: http://www.developerfusion.co.uk/utilities/convertvbtocsharp.aspx http://www.dotne ...
- 移动端微信应用开发总结(function ajax meta)
关键字:document function ajax jquery html5 meta 微信应用开发时,特意把各个容易混淆的知识点和要点,梳理后记录下来,也分享给各位.有问题还请多指正. 一 文档载 ...
- mysql函数find_in_set()
SELECT FIND_IN_SET('b','a,b,c,d'); 结果:2 SELECT * from video where find_in_set(id,'1,2,3,4'); 查找id在‘1 ...
- CI框架 -- URL
移除 URL 中的 index.php 默认情况,你的 URL 中会包含 index.php 文件: example.com/index.php/news/article/my_article 如果你 ...
- Android开发之获取相册照片和获取拍照照片二
转至 http://blog.csdn.net/beyond0525/article/details/8940840 上一篇文章中讲解了照相机获取照片的时候遇到了可能取得的uri为null的状态,并给 ...