thymeleaf模板引擎
Thymeleaf是一个适用于Web和独立环境的现代服务器端Java模板引擎。Spring Boot 推荐使用的模板引擎就是Thymeleaf。
引入Thymeleaf
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
Thymeleaf使用和语法
@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页面放在classpath:/templates/下,thymeleaf就会自动渲染
页面:
controlle:
@Controller
public class HelloController {
@RequestMapping("/success")
public String success(Map<String, Object> map) {
map.put("hello", "你好,thymeleaf");
return "success";
}
}
语法请参照thymeleaf官网
想要在写thymeleaf表达式的时候有提示,就需要导入thymeleaf的名称空间
<!--xmlns:th="http://www.thymeleaf.org" 就是这一句导入thymeleaf的名称空间-->
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>成功</title>
</head>
<body>
<h1>成功</h1>
<div th:text="${hello}"></div>
</body>
</html>
语法
可以使用th替换标签的任意属性。
在使用文档的第10章有
特性 | 属性 | 描述 |
---|---|---|
Fragment inclusion | th:insert | 片段包含,插入,相当于jsp的include |
th:replace | 片段包含,替换 | |
Fragment iteration | th:each | 遍历,相当于jsp的c:forEach |
Conditional evaluation | th:if | 判断,相当于jsp的c:if |
th:unless | ||
th:switch | ||
th:case | ||
Local variable definition | th:object/td> | 声明变量 |
th:with | ||
General attribute modification | th:attr | 修改任意属性 |
th:attrprepend | 在属性值前添加值 | |
th:attrappend | 在属性值后添加值 | |
Specific attribute modification | th:value | 特定属性的修改 |
th:href | ||
th:src | ||
... | ||
Text (tag body modification) | th:text | 修改标签体内容,不转义 |
th:utext | 修改标签体内容,转义 | |
Fragment specification | th:fragment | 指定片段 |
Fragment removal | th:remove | 移除片段 |
-
Simple expressions:(表达式语法)
- Variable Expressions: ${…}
- OGNL:
- 获取对象的属性,调用方法
- 使用内置对象
- 内置的工具对象
- OGNL:
- Selection Variable Expressions: *{…}
- 变量的选择表达式:和
${}
在功能上是一样的- 配合
th:object="${session.user}"
使用
- 配合
- 变量的选择表达式:和
- Message Expressions: #{…}
- 获取国际化内容的
- Link URL Expressions: @{…}
- 定义URL连接的
- Fragment Expressions: ~{…}
- 片段引用
- Variable Expressions: ${…}
-
Literals(字面量)
- Text literals: ‘one text’ , ‘Another one!’ ,…
- Number literals: 0 , 34 , 3.0 , 12.3 ,…
- Boolean literals: true , false
- Null literal: null
- Literal tokens: one , sometext , main ,…
-
Text operations:(文本操作)
- String concatenation: +
- 字符串拼接
- Literal substitutions: |The name is ${name}|
- 字符串替换
- String concatenation: +
-
Arithmetic operations:(数学运算)
- Binary operators: + , - , * , / , %
- Minus sign (unary operator): -
-
Boolean operations:(布尔运算)
- Binary operators: and , or
- Boolean negation (unary operator): ! , not
-
Comparisons and equality:(比较运算)
- Comparators: > , < , >= , <= ( gt , lt , ge , le )
- Equality operators: == , != ( eq , ne )
-
Conditional operators:(条件运算)
- If-then: (if) ? (then)
- If-then-else: (if) ? (then) : (else)
- 三元运算
- Default: (value) ?: (defaultvalue)
-
Special tokens:(特殊操作)
- No-Operation: _
- 没有操作:(不做任何操作)
- (1 < 2) ? _ : false
- 没有操作:(不做任何操作)
- No-Operation: _
示例:
@RequestMapping("/success")
public String success(Map<String, Object> map) {
map.put("hello", "你好,thymeleaf");
map.put("h1", "<h1>你好啊!!</h1>");
map.put("users", Arrays.asList("张三", "李四", "王五"));
return "success";
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>成功</title>
</head>
<body>
<h1>成功</h1>
<div th:text="${hello}"></div>
<hr />
<div th:text="${h1}"></div>
<div th:utext="${h1}"></div>
<hr />
<h1 th:text="${user}" th:each="user: ${users}"></h1>
<hr />
<h2>
<span th:each="user : ${users}">[[${user}]] </span>
</h2>
</body>
</html>
结果: