当前位置: 移动技术网 > IT编程>开发语言>Java > SpringBoot——Web开发(静态资源映射)

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

2019年09月12日  | 移动技术网IT编程  | 我要评论

静态资源映射

springboot对于springmvc的自动化配置都在webmvcautoconfiguration类中。

1568273415096

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

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

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

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

/

小结:

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

classpath:/meta-inf/resources/

classpath:/resources/

classpath:/static/

classpath:/public/

/

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

自定义favicon,自定义

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

1568275953018

1568275943433

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

1568276104502

1568276090522

定制banner

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

1568277030901

代码参考:

https://github.com/hcj-shadow/springbootplus

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网