Thymeleaf模板语法与表达式
添加依赖
1 <dependency> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-thymeleaf</artifactId> 4 </dependency> 5 <dependency> 6 <groupId>org.thymeleaf</groupId> 7 <artifactId>thymeleaf</artifactId> 8 </dependency>
使用前须添加命名空间
<html xmlns:th="http://www.thymeleaf.org">
标准表达式语法
我们将在杂货店虚拟商店的开发中稍作休息,以了解Thymeleaf标准方言中最重要的部分之一:Thymeleaf Standard Expression语法。
我们已经看到了用这种语法表达的两种类型的有效属性值:消息表达式和变量表达式:
1 <p th:utext="#{home.welcome}">Welcome to our grocery store!</p>
2
3 <p>Today is: <span th:text="${today}">13 february 2011</span></p>
但是还有更多类型的表达式,以及更多有趣的细节来了解我们已经知道的表达式。首先,让我们看一下标准表达式功能的快速摘要:
- 简单表达式:
- 变量表达式:
${...} - 选择变量表达式:
*{...} - 消息表达式:
#{...} - 链接网址表达式:
@{...} - 片段表达式:
~{...}
- 变量表达式:
- 文字
- 文本文本:、,...
'one text''Another one!' - 数字文本:、 、 、 ,...
0343.012.3 - 布尔文本: ,
truefalse - 空文字:
null - 文字标记:、 、 ,...
onesometextmain
- 文本文本:、,...
- 文本操作:
- 字符串串联:
+ - 文字替换:
|The name is ${name}|
- 字符串串联:
- 算术运算:
- 二元运算符:、 、 、 、
+-*/% - 减号(一元运算符):
-
- 二元运算符:、 、 、 、
- 布尔运算:
- 二元运算符:,
andor - 布尔否定(一元运算符): ,
!not
- 二元运算符:,
- 比较和平等:
- 比较器: , , , (, , ,
><>=<=gtltgele) - 相等运算符:、 (、
==!=eqne)
- 比较器: , , , (, , ,
- 条件运算符:
- 如果-那么:
(if) ? (then) - if-then-else:
(if) ? (then) : (else) - 默认:
(value) ?: (defaultvalue)
- 如果-那么:
- 特殊令牌:
- 无操作:
_
- 无操作:
所有这些功能都可以组合和嵌套:
1 'User is of type ' + (${user.isAdmin()} ? 'Administrator' : (${user.type} ?: 'Unknown'))
| 次序 | 特征 | 属性 |
|---|---|---|
| 1 | 片段包含 | th:insertth:replace |
| 2 | 片段迭代 | th:each |
| 3 | 条件评估 | th:ifth:unlessth:switchth:case |
| 4 | 局部变量定义 | th:objectth:with |
| 5 | 常规属性修改 | th:attrth:attrprependth:attrappend |
| 6 | 特定属性修改 | th:valueth:hrefth:src... |
| 7 | 文本(标签正文修改) | th:textth:utext |
| 8 | 片段规范 | th:fragment |
| 9 | 片段删除 | th:remove |
| Order | Feature | Attributes |
|---|---|---|
| 1 | Fragment inclusion | th:insertth:replace |
| 2 | Fragment iteration | th:each |
| 3 | Conditional evaluation | th:ifth:unlessth:switchth:case |
| 4 | Local variable definition | th:objectth:with |
| 5 | General attribute modification | th:attrth:attrprependth:attrappend |
| 6 | Specific attribute modification | th:valueth:hrefth:src... |
| 7 | Text (tag body modification) | th:textth:utext |
| 8 | Fragment specification | th:fragment |
| 9 | Fragment removal | th:remove |
- Simple expressions:
- Variable Expressions:
${...} - Selection Variable Expressions:
*{...} - Message Expressions:
#{...} - Link URL Expressions:
@{...} - Fragment Expressions:
~{...}
- Variable Expressions:
- Literals
- Text literals: , ,…
'one text''Another one!' - Number literals: , , , ,…
0343.012.3 - Boolean literals: ,
truefalse - Null literal:
null - Literal tokens: , , ,…
onesometextmain
- Text literals: , ,…
- Text operations:
- String concatenation:
+ - Literal substitutions:
|The name is ${name}|
- String concatenation:
- Arithmetic operations:
- Binary operators: , , , ,
+-*/% - Minus sign (unary operator):
-
- Binary operators: , , , ,
- Boolean operations:
- Binary operators: ,
andor - Boolean negation (unary operator): ,
!not
- Binary operators: ,
- Comparisons and equality:
- Comparators: , , , (, , ,
><>=<=gtltgele) - Equality operators: , (,
==!=eqne)
- Comparators: , , , (, , ,
- Conditional operators:
- If-then:
(if) ? (then) - If-then-else:
(if) ? (then) : (else) - Default:
(value) ?: (defaultvalue)
- If-then:

浙公网安备 33010602011771号