版权声明:dream_on_sakura_rain https://blog.csdn.net/qq_32112175/article/details/88060432
Thymeleaf学习教程
介绍
- Thymeleaf是一种用于Web和独立环境的现代服务器端的Java模板引擎。
- Thymeleaf能够处理HTML,XML,JavaScript,CSS甚至纯文本。
直接介绍方言,实战
大致分为五种
- ${…} : 变量表达式。
- 从变量中实例化数据信息
- *{…} : 选择表达式。
- 上下文对象中获取变量信息
- #{…} : 消息 (i18n) 表达式。
- 在Spring应用程序中,它将自动与Spring的MessageSource机制集成,package org.thymeleaf.expression包中class进行数据处理
- @{…} : 链接 (URL) 表达式。
- 链接表达式在构建URL并向其添加有用的上下文和会话信息(通常称为URL重写的过程)。
- ~{…} : 片段表达式。
- 支持片段的引入效果,一般是模板的引入配合th:insert th:replace使用
文字和操作
-
文字
- 文本文字,例如:‘one text’, ‘Another one!’,…
- 数字文字,例如:0,10, 314, 31.01, 112.83,…
- 布尔文字,例如:true,false
- Null文字,例如:Null
- 文字标记,例如:one, sometext, main,…
-
文本操作:
- 字符串连接:+
- 文字替换:|The name is ${name}|
-
算术运算:
- 二进制操作:+, -, *, /, %
- 减号(一元运算符):-
-
布尔运算:
- 二进制运算符,and,or
- 布尔否定(一元运算符):!,not
-
比较和相等:
- 比较运算符:>,<,>=,<=(gt,lt,ge,le)
- 相等运算符:==, != (eq, ne)
-
条件操作符:
- If-then:(if) ? (then)
- If-then-else:(if) ? (then) : (else)
- Default: (value) ?: (defaultvalue)
表达式的预处理
- 注意事项:
所有的预处理操作实在_(预处理位置)_进行的#{selection.__${sel.code}__}
字符转义
- 使用方式
<div th:text="${html}">Some escaped text</div>
<div th:utext="${html}">Some unescaped text</div>
迭代的使用
<tr th:each="sb : ${allSeedStarters}">
<td th:text="${{sb.datePlanted}}">13/01/2011</td>
<td th:text="#{|bool.${sb.covered}|}">yes</td>
<td th:text="#{|seedstarter.type.${sb.type}|}">Wireframe</td>
<td th:text="${#strings.arrayJoin(
#messages.arrayMsg(
#strings.arrayPrepend(sb.features,'seedstarter.feature.')),
', ')}">Electric Heating, Turf</td>
<td>
<table>
<tbody>
<tr th:each="row,rowStat : ${sb.rows}">
<td th:text="${rowStat.count}">1</td>
<td th:text="${row.variety.name}">Thymus Thymi</td>
<td th:text="${row.seedsPerCell}">12</td>
</tr>
</tbody>
</table>
</td>
</tr>
表单中使用的
- th:action 表单提交请求地址
- th:object 后台接收对象
- th:field 对象中的属性