SpringBoot学习-part19 webjars和静态资源配置

    技术2022-07-11  82

    WebMvcAutoConfiguration.java中的关键源码

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

    @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)); } }

    /**访问当前项目的任何资源,

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

    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:

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

    为啥写favicon.ico都不行的,非要改名加个1啥的才可以?

    谷歌清空缓存测试 favicon.ico

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

    Processed: 0.017, SQL: 9