Spring Boot的默认静态资源的路径为:
spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/
优先级从从高到低
一、Spring Boot 访问静态资源有二种方案
第一种方案(默认):Spring Boot从classpath/static的目录下面获取静态资源
目录名称必须要叫static
第一种形式:
- 在resource下面创建static目录,添加a.jpg
- 浏览器访问:http://localhost:8080/a.jpg
- 如果静态资源放到不同的路径,访问时需要加上路径
- 在 static下面创建images文件夹,把b.jpg复制即可
- 浏览器访问:localhost:8080/images/b.jpg
第二种形式: - 新建index.html页面,从页面中获取静态与资源
在resource/static/和resource/static/images目录下面把index.html页面复制进去。 - resource/static/index.html内容为:
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>静态资源访问方式1</title>
</head>
<body>静态资源访问方式1....<hr/><img alt="gblfy" src="a.jpg">
</body>
</html>
- resource/static/images/index.html内容为:
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>静态资源访问方式2</title>
</head>
<body>静态资源访问方式2....<hr/><img alt="gblfy" src="b.jpg">
</body>
</html>
- 浏览器测试:
第二种方案:
ServletContext的根目录下:在 src/main/webapp 目录名称必须要叫webapp,
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>springboot整合静态资源方式1</title>
</head>
<body>
springboot整合静态资源方式1
<hr/>
<img alt="gblfy" src="/images/1.jpg">
</body>
</html>
本文源码下载:
github地址:
https://github.com/gb-heima/Spring-Boot-Actual-Combat/tree/master/parent/spring-boot-chapter-6