静态资源:
由springboot启动原理我们知道 所有的启动项都有autoConfiguration
我们看看资源autoConfiguration 和web有关的东西
发现了webMVCAutoConfiguration
,资源是resource 所以我们找到了资源管理类。
通过addResourceHandlers进行静态资源加载
第一个if:
如果有自定义的资源处理方式 则不再进行加载
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();
...
...
}
第2个if
开始加载webjars的资源
// 从registry看有没有/webjars 有的话加载webjars里面的源文件。
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));
}
我们如果引入webjars的包如jQuery,通过以上源码可以判断我们可以通过地址访问得到该包下里面的js文件。
小结:我们的很多网页静态默认静态资源通过webjars引入我们的项目。这个更多的是第三方jar。
第3个if:
我们自己的静态资源引入
String staticPathPattern = this.mvcProperties.getStaticPathPattern();
// 看看staticPathPattern下面有没有静态资源
if (!registry.hasMappingForPattern(staticPathPattern)) {
// 存在资源 相关路径下的资源添加到Cache里面。
this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{staticPathPattern}).addResourceLocations(WebMvcAutoConfiguration.getResourceLocations(this.resourceProperties.getStaticLocations())).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
}
}
我们再看看staticPathPattern里面是什么
发现这个是ResourceProperties 下的一个属性:
public class ResourceProperties {
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};
// 就是这个属性
private String[] staticLocations;
private boolean addMappings;
private final ResourceProperties.Chain chain;
private final ResourceProperties.Cache cache;
// 构造器里面将静态常量CLASSPATH_RESOURCE_LOCATIONS 付给了staticLocations
public ResourceProperties() {
this.staticLocations = CLASSPATH_RESOURCE_LOCATIONS;
this.addMappings = true;
this.chain = new ResourceProperties.Chain();
this.cache = new ResourceProperties.Cache();
构造器里面将CLASSPATH_RESOURCE_LOCATIONS
的值付给了他,意味着staticPathPattern
指向了
"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"
这些地址。也就说我们的静态资源只要放在这个路径下也就会被springboot扫描到,就能正常使用了。
thymeleaf模板引擎加载
我们留意到,springboot的资源目录里有template,这里面没有加载这个模板。我们再看看是怎么加载这个的。
我们用thymeleaf作为我们的模板引擎。(可以在建项目的时候进行勾选)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
(小知识:这个是springboot的启动类依赖,他是引入了thymeleaf的官方依赖。)
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
<version>3.0.11.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-java8time</artifactId>
<version>3.0.4.RELEASE</version>
<scope>compile</scope>
</dependency>
用相同的思路----> 既然是springboot项目就可以通过找autocofiguration找到他的启动项
我们找到了ThymeleafAutoConfiguration
@EnableConfigurationProperties({ThymeleafProperties.class})
@ConditionalOnClass({TemplateMode.class, SpringTemplateEngine.class})
@AutoConfigureAfter({WebMvcAutoConfiguration.class, WebFluxAutoConfiguration.class})
public class ThymeleafAutoConfiguration {
AutoConfigureAfter 这个注解我们分开看,前面是Autoconfigure后面是after,我们可以想到是在加载了后面两个类之后,在进行这个类的加载,这也符合web的要求,即先加载WebMvcAutoConfiguration,在加载我们的引擎模板。意味着我们的引擎模板需要有WebMvcAutoConfiguration作为先决条件,我们知道,引擎模板是将今天网页和数据拼接的一个东西,所以这个自动配置可以通过WebFluxAutoConfiguration得到我们想要传到前端的值。
小彩蛋:
里面有很多的静态内部类
@ConditionalOnClass({Java8TimeDialect.class})
static class ThymeleafJava8TimeDialect {
--------------------------------
@ConditionalOnClass({SpringSecurityDialect.class})
static class ThymeleafSecurityDialectConfiguration
--------------------------------
@ConditionalOnClass({LayoutDialect.class})
static class ThymeleafWebLayoutConfiguration
....
我们由注解可以知道,他是在判断环境,也就是说他可以在很多的环境下使用,不同的环境加载不同的静态类。这也就是thymeleaf使用各种环境的原因。
上面有我们熟悉的EnableConfigurationProperties注解,这里面试可以配置的属性
@ConfigurationProperties(
prefix = "spring.thymeleaf"
)
public class ThymeleafProperties {
private static final Charset DEFAULT_ENCODING;
public static final String DEFAULT_PREFIX = "classpath:/templates/";
public static final String DEFAULT_SUFFIX = ".html";
private boolean checkTemplate = true;
private boolean checkTemplateLocation = true;
private String prefix = "classpath:/templates/";
private String suffix = ".html";
private String mode = "HTML";
private Charset encoding;
private boolean cache;
private Integer templateResolverOrder;
private String[] viewNames;
private String[] excludedViewNames;
private boolean enableSpringElCompiler;
private boolean renderHiddenMarkersBeforeCheckboxes;
private boolean enabled;
private final ThymeleafProperties.Servlet servlet;
private final ThymeleafProperties.Reactive reactive;
前缀是spring.thymeleaf,
public static final String DEFAULT_PREFIX = "classpath:/templates/";
public static final String DEFAULT_SUFFIX = ".html";
private String prefix = "classpath:/templates/";
private String suffix = ".html";
private String mode = "HTML";
可以帮我们加载根路径下的.html文件。
通过修改路径:
thymeleaf:
prefix: classpath:/mybatis/
回报找不到模板的错误
org.thymeleaf.exceptions.TemplateInputException: Error resolving template [login], template might not exist or might not be accessible by any of the configured Template Resolvers