🤖🤖-摘要:
本文介绍了SpringBoot Web开发中的模板引擎,包括SpringMVC所支持的多种模板技术,如Thymelaf、FreeMarker和JSP等,以及SpringBoot自动配置支持的模板引擎。同时还详细阐述了如何整合Thymeleaf。

模板引擎

除了RESTWeb服务,您还可以使用SpringMVC来提供动态HTML内容。
SpringMVC支持多种模板技术,包括Thymelaf、FreeMarker和JSP。
此外,许多其他模板引擎也包含了它们自己的SpringMVC集成。

SpringBoot自动配置支持的模板引擎有:

整合Thymeleaf

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

由自动配置原理知:

  1. 开启了org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration自动配置
  2. 属性绑定在ThymeleafProperties中,对应配置文件spring.thymeleaf内容
  3. 默认情况,模板页面在classpath:/templates文件夹下
  4. 默认情况,模板页面前缀classpath:/templates/
  5. 默认情况,模板页面后缀.html
  6. 达到效果: classpath:/templates/ + 页面名 + .html

目前大多数是前后分离开发,由专业的前端框架完成,模板引擎部分用处不大.