、导入版本信息跟依赖
<properties> <thymeleaf.version>3.0.9.RELEASE</thymeleaf.version> <thymeleaf-layout-dialect.version>2.2.2</thymeleaf-layout-dialect.version> </properties>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
2、Thymeleaf使用
1 @ConfigurationProperties(prefix = "spring.thymeleaf") 2 public class ThymeleafProperties { 3 private static final Charset DEFAULT_ENCODING = Charset.forName("UTF‐8"); 4 private static final MimeType DEFAULT_CONTENT_TYPE = MimeType.valueOf("text/html"); 5 public static final String DEFAULT_PREFIX = "classpath:/templates/"; 6 public static final String DEFAULT_SUFFIX = ".html"; 7 //
只要我们把HTML页面放在classpath:/templates/,thymeleaf就能自动渲染;
使用:
1、导入thymeleaf的名称空间
1 <html lang="en" xmlns:th="http://www.thymeleaf.org">
2、使用thymeleaf语法;
1 <!DOCTYPE html> 2 <html lang="en" xmlns:th="http://www.thymeleaf.org"> 3 <head> 4 <meta charset="UTF‐8"> 5 <title>Title</title> 6 </head> 7 <body> 8 <h1>成功!</h1> 9 <!‐‐th:text 将div里面的文本内容设置为 ‐‐> 10 <div th:text="${hello}">这是显示欢迎信息</div> 11 </body> 12 </html>
3、语法规则
1)、th:text;改变当前元素里面的文本内容;
th:任意html属性;来替换原生属性的值