Thymeleaf 模板引擎 1. 引入Thymeleaf <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> @ConfigurationProperties(prefix = "spring.thymeleaf") public class ThymeleafProperties { private static final Charset DEFAULT_ENCODING = StandardCharsets.UTF_8; public static final String DEFAULT_PREFIX = "classpath:/templates/"; public static final String DEFAULT_SUFFIX = ".html"; 将html页面放在templates目录下使用模板引擎 渲染页面
html中引入
<html lang="en" xmlns:th="http://www.thymeleaf.org"> 2. 语法规则 来自官网的使用手册:http://www.thymeleaf.org
th 语法 表达式
Simple expressions:(表达式语法) Variable Expressions: ${...}:获取变量值;OGNL; 1)、获取对象的属性、调用方法 2)、使用内置的基本对象: #ctx : the context object. #vars: the context variables.
1. 开始使用 创建springBoot 应用 选择需要的模块 创建springboot
选择模块
SpringBoot 已经为我们把需要的配置自动装配好,只需要在后续的开发中,在配置文件中进行设置即可
编写代码
2. 自动配置 XXXXXAutoConfiguration :在容器中自动配置组件 XXXXXProperties : 配置类 这些文件都在通过Mavan引入的外部库中,org.springframework.boot.autoconfigure。
3. 静态资源映射规则 默认创建的 SpringBoot 项目是没有web文件夹的,那如何映射css,js等静态资源呢?
Spring boot 会自动装配WebMvcAutoConfiguration类,会对静态资源的映射做出规定,
@Override public 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(); if (!registry.hasMappingForPattern("/webjars/**")) { customizeResourceHandlerRegistration(registry.addResourceHandler("/webjars/**") .addResourceLocations("classpath:/META-INF/resources/webjars/") .setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl)); } String staticPathPattern = this.mvcProperties.getStaticPathPattern(); if (!