页面加入命名空间
<html lang="en" xmlns:th="http://www.thymeleaf.org">
表达式:
- ${}用的最多也是最强大的表达式,获取变量值;底层是OGNL;
- Selection Variable Expressions: *{…}:选择表达式:和${}在功能上是一样;
- Message Expressions: #{…}:获取国际化内容
- Link URL Expressions: @{…}:定义URL
@{/order/process(execId=${execId},execType='FAST')}
- Fragment Expressions: ~{…}:片段引用表达式,引入另一个界面
<div th:insert="~{commons :: main}">...</div>
表达式支持的语法:
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}|
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: _
综合案例:
<!DOCTYPE html>
<!--导入名称空间-->
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>成功</h1>
<!--th:text 将div的文本内容设置为-->
<div id="div01" class="myDiv" th:id="${hello}" th:class="${hello}" th:text="${hello}">这里显示的是欢迎信息</div>
<br>
<!--th:utext 不会转义特殊字符,会直接做出来一个h1标签,但是text不会,会显示<h1>你好</h1>-->
<div th:text="${hello}">这里显示的是欢迎信息</div>
<div th:utext="${hello}">这里显示的是欢迎信息</div>
<br>
<!--遍历-->
<h4 th:text="${user}" th:each="user:${users}"></h4>
<br>
<!--行内写法-->
<h4>
<span th:each="user:${users}">[[${user}}]]</span>
</h4>
</body>
</html>