第一部分:Thymeleaf介绍

简单说,Thymeleaf 是一个跟 Velocity、FreeMarker 类似的模板引擎,它可以完全替代 JSP 。相较与其他的模板引擎,它有如下三个极吸引人的特点:
1.Thymeleaf 在有网络和无网络的环境下皆可运行,即它可以让美工在浏览器查看页面的静态效果,也可以让程序员在服务器查看带数据的动态页面效果。这是由于它支持 html 原型,然后在 html 标签里增加额外的属性来达到模板+数据的展示方式。浏览器解释 html 时会忽略未定义的标签属性,所以 Thymeleaf 的模板可以静态地运行;当有数据返回到页面时,Thymeleaf 标签会动态地替换掉静态内容,使页面动态显示。
2.Thymeleaf 开箱即用的特性。它提供标准和 Spring 标准两种方言,可以直接套用模板实现 JSTL、 OGNL表达式效果,避免每天套模板、改 Jstl、改标签的困扰。同时开发人员也可以扩展和创建自定义的方言。
3.Thymeleaf 提供 Spring 标准方言和一个与 SpringMVC 完美集成的可选模块,可以快速的实现表单绑定、属性编辑器、国际化等功能。

第二部分:常用命令介绍

utext及text使用
th:utext和th:text的区别是:th:text会对<和>进行转义,而th:utext不会转义。

eg.. 国际化文件中 welcome.info=欢迎您{0}登录系统!

1
2
#admin 页面中变量
<h2 th:text="#{welcome.info('admin')}"/>

eg. utext进行一下简单运算

1
<p th:text="'数学计算:1+2=' + (1 + 2)"/>

$及*区别及使用

1
2
3
4
5
6
7
8
9
10
11
12
13
 # user为后台传输对象 
<div>
<p th:text="'用户姓名:' + ${user.name}"/>
<p th:text="'用户年龄:' + ${user.age}"/>
<p th:text="'出生日期:' + ${#dates.format(user.birthday,'yyyy-MM-dd')}"/>
</div>

<hr/>
<div th:object="${user}">
<p th:text="'用户姓名:' + *{name}"/>
<p th:text="'用户年龄:' + *{age}"/>
<p th:text="'出生日期:' + *{#dates.format(birthday,'yyyy-MM-dd')}"/>
</div>

日期、字符串、集合使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<body>
<p th:text="${#dates.format(mydate,'yyyy-MM-dd')}"/>
<p th:text="${#dates.format(mydate,'yyyy-MM-dd HH:mm:ss.SSS')}"/>
<hr/>
<p th:text="${#strings.replace('www.baidu.cn','.','$')}"/>
<p th:text="${#strings.toUpperCase('www.baidu.cn')}"/>
<p th:text="${#strings.trim('www.baidu.cn')}"/>
<hr/>
<p th:text="${#sets.contains(names,'boot-0')}"/>
<p th:text="${#sets.contains(names,'boot-9')}"/>
<p th:text="${#sets.size(names)}"/>
<hr/>
<p th:text="${#sets.contains(ids,0)}"/>
<p th:text="${ids[1]}"/>
<p th:text="${names[1]}"/>
</body>

@{}使用

1
2
3
<script type="text/javascript" th:src="@{/js/main.js}"></script> 
<a th:href="@{/show}">访问controller方法</a>
<a th:href="@{/static_index.html}">访问静态页面</a>

操作内置对象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>SpringBoot模版渲染</title>
<script type="text/javascript" th:src="@{/js/main.js}"></script>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
</head>
<body>
<p th:text="${#httpServletRequest.getRemoteAddr()}"/>
<p th:text="${#httpServletRequest.getAttribute('requestMessage')}"/>
<p th:text="${#httpSession.getId()}"/>
<p th:text="${#httpServletRequest.getServletContext().getRealPath('/')}"/>
<hr/>
<p th:text="'requestMessage = ' + ${requestMessage}"/>
<p th:text="'sessionMessage = ' + ${session.sessionMessage}"/>
<p th:text="'applicationMessage = ' + ${application.applicationMessage}"/>
</body>
</html>

逻辑处理
and、or、关系比较(>、<、>=、<=、==、!=、lt、gt、le、ge、eq、ne)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#满足情况下th:if
<span th:if="${member.age lt 18}">
未成年人!
</span>
<span th:if="${member.name eq '啊三'}">
欢迎小三来访问!
</span>

#不满足情况下th:unless
<span th:unless="${member.age gt 18}">
你还不满18岁,不能够看电影!
</span>

#switch选择语句th:switch
<span th:switch="${member.uid}">
<p th:case="100">uid为101的员工来了</p>
<p th:case="99">uid为102的员工来了</p>
<p th:case="*">没有匹配成功的数据!</p>
</span>

数据遍历

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#list集合遍历
<body>
<table>
<tr><td>No.</td><td>UID</td><td>姓名</td><td>年龄</td><td>偶数</td><td>奇数</td></tr>
<tr th:each="user,memberStat:${allUsers}">
<td th:text="${memberStat.index + 1}"/>
<td th:text="${user.uid}"/>
<td th:text="${user.name}"/>
<td th:text="${user.age}"/>
<td th:text="${memberStat.even}"/>
<td th:text="${memberStat.odd}"/>
</tr>
</table>
</body>

#map集合遍历
<body>
<table>
<tr><td>No.</td><td>KEY</td><td>UID</td><td>姓名</td><td>年龄</td><td>偶数</td><td>奇数</td></tr>
<tr th:each="memberEntry,memberStat:${allUsers}">
<td th:text="${memberStat.index + 1}"/>
<td th:text="${memberEntry.key}"/>
<td th:text="${memberEntry.value.uid}"/>
<td th:text="${memberEntry.value.name}"/>
<td th:text="${memberEntry.value.age}"/>
<td th:text="${memberStat.even}"/>
<td th:text="${memberStat.odd}"/>
</tr>
</table>
</body>

#数字遍历
# eg. 比如我们要写一个月份的下拉列表
<option th:each="i:${#numbers.sequence(1, 28)}" th:value="${i}" th:utext="'每月'+${i}+'号'"></option>

页面引入

关于thymeleaf th:replace th:include th:insert 的区别
th:insert :保留自己的主标签,保留th:fragment的主标签。
th:replace :不要自己的主标签,保留th:fragment的主标签。
th:include :保留自己的主标签,不要th:fragment的主标签。(官方3.0后不推荐)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
需要替换的片段内容:
<footer th:fragment="copy">
<script type="text/javascript" th:src="@{/plugins/jquery/jquery-3.0.2.js}"></script>
</footer>

导入片段:
<div th:insert="footer::copy"></div>
<div th:replace="footer::copy"></div>
<div th:include="footer::copy"></div>

结果为:
<div>
<footer>
<script type="text/javascript" th:src="@{/plugins/jquery/jquery-3.0.2.js}"></script>
</footer>
</div>
<footer>
<script type="text/javascript" th:src="@{/plugins/jquery/jquery-3.0.2.js}"></script>
</footer>
<div>
<script type="text/javascript" th:src="@{/plugins/jquery/jquery-3.0.2.js}"></script>
</div>