上一节:pringboot开发笔记-(10)-日志框架SLF4J和logback等
11.1 动态资源引入源码解析
动态资源引入, springboot webmvc 在 WebMvcAutoConfiguration源码中有两种形式:
webjars引入动态资源:
映射形式:
/webjars/**
======>classpath:/META-INF/resources/webjars
/** 指定的几个目录
映射形式:
"/**"
======> classpath: [/META-INF/resources/, /resources/, /static/, /public/]- META-INF/resources/
- /resources/
- /static/
- /public/
详见: WebMvcAutoConfiguration
源码的解析:
@Overridepublic 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(); // #1. "webjars"目录映射=> classpath:/META-INF/resources/webjars/ if (!registry.hasMappingForPattern("/webjars/**")) { customizeResourceHandlerRegistration(registry.addResourceHandler("/webjars/**") // 对应下面的 jquery.js目录: .addResourceLocations("classpath:/META-INF/resources/webjars/") .setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl)); } // #2."/**"目录映射 => classpath:[/META-INF/resources/, /resources/, /static/, /public/] String staticPathPattern = this.mvcProperties.getStaticPathPattern(); if (!registry.hasMappingForPattern(staticPathPattern)) { customizeResourceHandlerRegistration(registry.addResourceHandler(staticPathPattern) .addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations())) .setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl)); }}
// 下面23行的: this.resourceProperties.getStaticLocations():
@ConfigurationProperties(prefix = "spring.resources", ignoreUnknownFields = false)public class ResourceProperties { private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { "classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/" }; /** * Locations of static resources. Defaults to classpath:[/META-INF/resources/, * /resources/, /static/, /public/]. */ private String[] staticLocations = CLASSPATH_RESOURCE_LOCATIONS; .....}
11.2 用webjars形式引入动态资源
webjars形式引入jquery: pom:
<!-- webjars形式引入 jquery --><dependency> <groupId>org.webjars</groupId> <artifactId>jquery</artifactId> <version>3.5.1</version></dependency>
引入后jquery.js所在jar包构造:
而后在浏览器拜访: http://localhost:8080/webjars/jquery/3.5.1/jquery.js 即可拜访jquery.js的源码;
11.3 用static目录等形式引入动态资源
间接将动态文件copy到四个目录之一都能够
- classpath: /META-INF/resources
- classpath: /resources/
- classpath: /static/
- classpath: /public/
11.3.1 classpath:/META-INF/resources
11.3.2 classpath:/resources/
11.3.3 classpath:/static/
11.3.4 classpath:/public/
咱们应用 classpath: /static/, 如图:
咱们浏览器拜访: http://localhost:8080/list.html
OK!!
11.4 小结
springboot webmvc拜访动态资源有2种形式: 引入maven依赖的webjars形式和动态资源默认四个目录形式.
- webjars形式: www.webjars.com 找到webjars依赖的pom, 导入, 而后依据目录拜访即可!
/**的四种默认地址: classpath:[ /META-INF/resources, /resources/, /static/, /public/ ]
当然下面的 /** 也能够本人指定一个默认动态资源的地址, 指定形式:
spring.resources.static-locations=classpath:/hello1/, classpath:/hello2/