SpringBoot静态资源映射的方法
SpringBoot静态资源映射的方法
这篇“SpringBoot静态资源映射的方法”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“SpringBoot静态资源映射的方法”文章吧。
静态资源映射
SpringBoot对于SpringMVC的自动化配置都在WebMVCAutoConfiguration类中。
其中一个静态内部类WebMvcAutoConfigurationAdapter实现了WebMvcConfigurer接口。(361)
WebMvcConfigurer接口中定义了addResourceHandlers处理静态资源的默认映射关系.(500)
addResourceHandlers在WebMvcAutoConfigurationAdapter类中实现
publicvoidaddResourceHandlers(ResourceHandlerRegistryregistry){if(!this.resourceProperties.isAddMappings()){logger.debug("Defaultresourcehandlingdisabled");}else{DurationcachePeriod=this.resourceProperties.getCache().getPeriod();CacheControlcacheControl=this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();if(!registry.hasMappingForPattern("/webjars/**")){this.customizeResourceHandlerRegistration(registry.addResourceHandler(newString[]{"/webjars/**"}).addResourceLocations(newString[]{"classpath:/META-INF/resources/webjars/"}).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));}StringstaticPathPattern=this.mvcProperties.getStaticPathPattern();if(!registry.hasMappingForPattern(staticPathPattern)){this.customizeResourceHandlerRegistration(registry.addResourceHandler(newString[]{staticPathPattern}).addResourceLocations(WebMvcAutoConfiguration.getResourceLocations(this.resourceProperties.getStaticLocations())).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));}}}
其中
this.resourceProperties.getStaticLocations()
返回静态资源的默认映射关系,
getStaticLocations()方法在ResourceProperties中定义
其中,
privatestaticfinalString[]CLASSPATH_RESOURCE_LOCATIONS=newString[]{"classpath:/META-INF/resources/","classpath:/resources/","classpath:/static/","classpath:/public/"};
classpath:/META-INF/resources/
classpath:/resources/
classpath:/static/
classpath:/public/
第五个默认的资源映射:在静态方法getResourceLocations中定义
/
小结:
默认情况下,可以在以下五个位置放置静态资源
classpath:/META-INF/resources/classpath:/resources/classpath:/static/classpath:/public//
【静态资源一般放在classpath:/static/目录下】
自定义favicon,自定义index.html
favicon.ico是浏览器左上角的图标,可以放在静态资源路径下或者类路径下,静态资源路径优先级高。
SpringBoot启动后默认在静态资源目录下寻找index.html,如果没有找到;就会去resource/templates目录下寻找index.html(使用Thymeleaf模板)
以上就是关于“SpringBoot静态资源映射的方法”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注恰卡编程网行业资讯频道。
推荐阅读
-
springboot实现基于aop的切面日志
本文实例为大家分享了springboot实现基于aop的切面日志的具体代码,供大家参考,具体内容如下通过aop的切面方式实现日志...
-
SpringBoot定时任务功能怎么实现
-
SpringBoot中的@Import注解怎么使用
-
SpringBoot整合Lombok及常见问题怎么解决
-
springboot图片验证码功能模块怎么实现
-
Springboot+SpringSecurity怎么实现图片验证码登录
-
SpringBoot注解的知识点有哪些
SpringBoot注解的知识点有哪些这篇“SpringBoot注...
-
SpringBoot2.x中management.security.enabled=false无效怎么解决
-
springboot怎么禁用某项健康检查
springboot怎么禁用某项健康检查今天小编给大家分享一下sp...
-
SpringBoot2怎么自定义端点