SpringBoot-thymeleaf 模板引擎初探

383 阅读1分钟

thymeleaf 模板引擎

  1. 导入依赖
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
  1. 使用
public class ThymeleafProperties {
//默认编码UTF-8
	private static final Charset DEFAULT_ENCODING = StandardCharsets.UTF_8;
//前缀
	public static final String DEFAULT_PREFIX = "classpath:/templates/";
//后缀
	public static final String DEFAULT_SUFFIX = ".html";
        }

由此可知访问的是/resources/templates/xxx.html,因此需要将文件放在该目录下

获取数据

html的元素都被thymeleaf接管,格式 th:元素名

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<header>
    <meta charset="utf-8">
    <title></title>
</header>
<body>
<h1 th:text="${msg}"></h1>

</body>
</html>