SpringBoot学习历程(七):集成Thymeleaf模板引擎

SpringBoot学习历程(七):集成Thymeleaf模板引擎

前言

  • 本人github仓库地址:https://github.com/RabbitsInTheGrass/springBoot-Learning
  • Spring Boot支持FreeMarker、Groovy、Thymeleaf和Mustache四种模板解析引擎,其中官方推荐使用Thymeleaf。

1. 引入依赖

<!-- 引入thymeleaf依赖 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

在Spring Boot中,默认的html页面地址为src/main/resources/templates,默认的静态资源地址为src/main/resources/static。

2. 配置文件支持属性

#THYMELEAF(ThymeleafAutoConfiguration)
spring.thymeleaf.cache=true #开启模板缓存(默认值:true)
spring.thymeleaf.check-template=true #是否在渲染之前检查模板是否存在。
spring.thymeleaf.check-template-location=true #检查模板位置是否正确(默认值:true)
spring.thymeleaf.content-type=text/html #Content-Type的值(默认值:text/html)
spring.thymeleaf.enabled=true #是否为Web框架启用Thymeleaf视图解析(默认值:true)
spring.thymeleaf.enable-spring-el-compiler = false #在SpringEL表达式中启用SpringEL编译器。
spring.thymeleaf.encoding=UTF-8 #模板编码
spring.thymeleaf.excluded-view-names= #要被排除在解析之外的视图名称列表,用逗号分隔
spring.thymeleaf.mode=HTML5 #要运用于模板之上的模板模式。另请参阅Thymeleaf的TemplateMode枚举(默认值:HTML5)
spring.thymeleaf.prefix=classpath:/templates/ #在构建URL时添加到视图名称前的前缀(默认值:classpath:/templates/)
spring.thymeleaf.reactive.chunked-mode-view-names = #逗号分隔的视图名称列表(允许的模式),当设置最大块大小时,应该是CHUNKED模式中唯一执行的视图名称列表。
spring.thymeleaf.reactive.full-mode-view-names =#即使设置了最大块大小,也应该在FULL模式下执行逗号分隔的视图名称列表(允许的模式)。
spring.thymeleaf.reactive.max-chunk-size = 0 #用于写入响应的数据缓冲区的最大大小(以字节为单位)。
spring.thymeleaf.reactive.media-types = #视图技术支持的媒体类型。
spring.thymeleaf.servlet.content-type = text / html #写入HTTP响应的Content-Type值。
spring.thymeleaf.suffix=.html #在构建URL时添加到视图名称后的后缀(默认值:.html)
spring.thymeleaf.template-resolver-order= 1 #Thymeleaf模板解析器在解析器链中的顺序。默认情况下,它排第一位。顺序从1开始,只有在定义了额外的TemplateResolver Bean时才需要设置这个属性。
spring.thymeleaf.view-names= #可解析的视图名称列表,用逗号分隔

一般我们将spring.thymeleaf.cache设置为false,其他保持默认值即可。

3. 示例

。。。so easy!

猜你喜欢

转载自blog.csdn.net/RabbitInTheGrass/article/details/102025366
今日推荐