七叶笔记 » java编程 » SpringBoot深入探究四种静态资源访问的方式

SpringBoot深入探究四种静态资源访问的方式

1.默认的静态资源目录

/static

/public

/resources

/META-INF/resources

动态资源目录:/templates

2.resources静态资源目录图片存放

3. 静态资源访问

3.1.通过路径访问静态资源

http://localhost:8080/a.jpg

http://localhost:8080/b.jpg

http://localhost:8080/c.png

http://localhost:8080/d.jpg

3.2.通过配置类配置路径访问本地静态资源

1.config

2.访问结果展示

路径:http://localhost:8080/mypic/huangshanpic.webp

3.3.通过配置文件配置路径访问静态资源

(1).application.yml

web.pic-path=C:/Users/Administrator/Desktop/images1/spring.mvc.static-path-pattern=/mypic/**spring.web.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/public/,classpath:/static/,file:${web.pic-path}

web.pic-path:访问路径

spring.mvc.static-path-pattern:采用全部映射到mypic路径的方式

spring.web.resources.static-locations:配置允许访问的静态资源目录

(2).访问路径格式

http://localhost:8080/mypic/a.jpg

http://localhost:8080/mypic/b.jpg

http://localhost:8080/mypic/c.png

http://localhost:8080/mypic/d.jpg

http://localhost:8080/mypic/web.pic-path配置本地路径下的图片名称

3.4.通过引入打包静态资源的jar包形式访问

(1).静态资源打jar包

创建一个新的web工程,只存放静态资源

1).pom.xml

2).静态资源目录结构

3).package点击打包

4).install到本地仓库

(2).主项目中引入依赖包

1).pom.xml

(3).路径访问

未引入webjars-locator-core的jar包:http://localhost:8080/webjars/example/版本号/huangshan.webp

引入webjars-locator-core的jar包:

http://localhost:8080/webjars/example/huangshan.webp

注意:如果主程序和引入打包的jar包静态资源下具备相同的目录结构,如:META-INF esourceswebjarsexample.0.1**,可能会出现路径访问失败的情况。

解决方法:clean主程序项目,重新运行。

(4).访问结果

到此这篇关于SpringBoot深入探究四种静态资源访问的方式的文章就介绍到这了,更多相关SpringBoot静态资源访问内容请搜索七叶笔记以前的文章或继续浏览下面的相关文章希望大家以后多多支持七叶笔记!

相关文章