我是靠谱客的博主 合适嚓茶,这篇文章主要介绍SpringBoot学习-part19 webjars和静态资源配置,现在分享给大家,希望可以做个参考。

WebMvcAutoConfiguration.java中的关键源码

所有/webjars/** 都去 classpath:/META-INF/resources/webjars/ 找资源

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@Override public void addResourceHandlers(ResourceHandlerRegistry registry) { if (!this.resourceProperties.isAddMappings()) { logger.debug("Default resource handling disabled"); return; } Duration cachePeriod = this.resourceProperties.getCache().getPeriod(); CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl(); if (!registry.hasMappingForPattern("/webjars/**")) { customizeResourceHandlerRegistration(registry.addResourceHandler("/webjars/**") .addResourceLocations("classpath:/META-INF/resources/webjars/") .setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl)); } String staticPathPattern = this.mvcProperties.getStaticPathPattern(); if (!registry.hasMappingForPattern(staticPathPattern)) { customizeResourceHandlerRegistration(registry.addResourceHandler(staticPathPattern) .addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations())) .setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl)); } }

在这里插入图片描述/**访问当前项目的任何资源,
在这里插入图片描述

在这里插入图片描述

/**访问当前项目的任何资源(静态资源的文件夹)

复制代码
1
2
3
4
5
6
7
classpath:/META-INF/resources/ classpath:/resources/ classpath:/static/ classpath:/public/ <!--当前项目的根路径--> /

测试访问js资源

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

欢迎页面映射

静态资源文件夹下的所有index.html页面,被 /** 映射:
在这里插入图片描述在public下添加index.html页面,测试:
在这里插入图片描述

webjars 以jar包 maven依赖方式引入外部资源

https://www.webjars.org/
在这里插入图片描述

引入webjar资源后的对应目录结构:

在这里插入图片描述

启动SpringBoot访问资源:

在这里插入图片描述更改SpringBoot页面的icon:

复制代码
1
2
3
4
在2.x以前的版本。直接将你需要的favicon.ico文件倒挡static下面就可以。 2.X以后的版本,取消了自动配置,需要人手动去在每一个页面添加???

为啥写favicon.ico都不行的,非要改名加个1啥的才可以?
在这里插入图片描述在这里插入图片描述

谷歌清空缓存测试 favicon.ico

在这里插入图片描述在这里插入图片描述

火狐更换端口到全新的8899测试favicon.ico

在这里插入图片描述

在这里插入图片描述

最后

以上就是合适嚓茶最近收集整理的关于SpringBoot学习-part19 webjars和静态资源配置的全部内容,更多相关SpringBoot学习-part19内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(107)

评论列表共有 0 条评论

立即
投稿
返回
顶部