前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SpringBoot——Web开发(静态资源映射)

SpringBoot——Web开发(静态资源映射)

作者头像
Noneplus
发布2019-09-24 16:17:34
4880
发布2019-09-24 16:17:34
举报
文章被收录于专栏:开发笔记开发笔记开发笔记

静态资源映射

SpringBoot对于SpringMVC的自动化配置都在WebMVCAutoConfiguration类中。

1568273415096
1568273415096

其中一个静态内部类WebMvcAutoConfigurationAdapter实现了WebMvcConfigurer接口。(361)

WebMvcConfigurer接口中定义了addResourceHandlers处理静态资源的默认映射关系.(500)

1568273579633
1568273579633

addResourceHandlers在WebMvcAutoConfigurationAdapter类中实现

public void addResourceHandlers(ResourceHandlerRegistry registry) {
            if (!this.resourceProperties.isAddMappings()) {
                logger.debug("Default resource handling disabled");
            } else {
                Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
                CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
                if (!registry.hasMappingForPattern("/webjars/**")) {
                    this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{"/webjars/**"}).addResourceLocations(new String[]{"classpath:/META-INF/resources/webjars/"}).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
                }

                String staticPathPattern = this.mvcProperties.getStaticPathPattern();
                if (!registry.hasMappingForPattern(staticPathPattern)) {
                    this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{staticPathPattern}).addResourceLocations(WebMvcAutoConfiguration.getResourceLocations(this.resourceProperties.getStaticLocations())).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
                }

            }
        }

其中

this.resourceProperties.getStaticLocations()

返回静态资源的默认映射关系,

1568274734576
1568274734576

getStaticLocations()方法在ResourceProperties中定义

其中,

private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};

classpath:/META-INF/resources/

classpath:/resources/

classpath:/static/

classpath:/public/

第五个默认的资源映射:在静态方法getResourceLocations中定义

1568275149287
1568275149287

/

小结:

默认情况下,可以在以下五个位置放置静态资源

classpath:/META-INF/resources/

classpath:/resources/

classpath:/static/

classpath:/public/

/

【静态资源一般放在classpath:/static/目录下】

自定义favicon,自定义index.html

favicon.ico是浏览器左上角的图标,可以放在静态资源路径下或者类路径下,静态资源路径优先级高。

1568275953018
1568275953018
1568275943433
1568275943433

SpringBoot启动后默认在静态资源目录下寻找index.html,如果没有找到;就会去resource/templates目录下寻找index.html(使用Thymeleaf模板)

1568276104502
1568276104502
1568276090522
1568276090522

定制banner

http://www.network-science.de/ascii/

创建banner.txt文件置于resource目录下

1568277030901
1568277030901

代码参考:

https://github.com/HCJ-shadow/SpringBootPlus

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-12 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 静态资源映射
  • 自定义favicon,自定义index.html
  • 定制banner
  • 代码参考:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档