什么是Thymelaf?
- 它是非常可拓展的。允许自定义名字来定义一组模板属性(或者甚至是标签)
- 它还带有一些称为标准方言的东西。它们定义了一组功能,这些功能满足大多数情况
标准表达式五种类型
${…}:变量表达式
*{…}:选择表达式
#{…}:消息表达式(i18n)
@{…}:链接表达式(URL)
~{…}:片段表达式
变量表达式:
<span th:text="${book.author.name}">
<li th:each="book:${books}">
选择表达式:
选择表达式和变量表达式一样。唯一区别是他们不是在上下文映射上执行,而是在先前选择的对象上执行。如:
<div th:object = "${session.user}">
<p>Name:<span th:text="*{firstName}"></span></p>
<p>Surname:<span th:text="*{lastName}"></span></p>
</div>
//等同于
<div>
<p>Name:<span th:text="${session.user.firstName}"></span></p>
<p>surName:<span th:text="${session.user.lastName}"></span></p>
</div>
消息表达式:
消息表达式通常称为文本外部化,国际化,允许从外部源(如:.properties)文件中检索特定语言环境的消息。在spring应用程序中,它将自动与spring的MessageSource机制集成。
#{main.title}
#{message.entrycreate(${entryId})}
链接表达式:
<script th:src="@{/script/jquery-3.1.1.js}"></script>
<link th:href="@{/css/bootstrap.css}">
<a href="@{/usethymeleaf(name=${name})}"></a>
片段表达式:
片段表达式是使用th:insert th:replace插入片段
<div th:insert="~{commons/head::main}"></div>//commons:上一级文件 head:当前文件名 //main:fragment中定义的名字
//或者
<div th:replace="commons/head::main"></div>
注:insert和replace区别:
th:replace不要自己主标签,保留th:fragment主标签
th:insert保留自己主标签,保留th:fragment主标签
实例
效果图
step:
- 引入提示(前提)
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
- thymeleaf布局
<head>
<meta charset="UTF-8">
<title>登录用户</title>
<head th:replace="common/head::static"/>
</head>
<!DOCTYPE html >
<html xmlns:th="http://www.thymeleaf.org">
<head th:fragment="static">
<link rel="stylesheet" href="/css/bootstrap.css">
<script type="text/javascript" src="/js/jquery.min.js" ></script>
<script type="text/javascript" src="/js/bootstrap.min.js"></script>
<script type="text/javascript" src="/css/toastr.min.css"></script>
<script type="text/javascript" src="/js/plugins/toastr/toastr.min.js"></script>
<script type="text/javascript"></script>
</head>
</html>
- 显示数据
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<button type="button" class="btn btn-default btn-success">修改</button>
<table class="table">
<thead>
<tr class="warning">
<th>id</th>
<th>name</th>
<th>password</th>
<th>comment</th>
<th>index</th>
<th>size</th>
<th>first</th>
<th>last</th>
<th>girlOrBoy</th>
</tr>
</thead>
<tbody>
<tr th:each="myList,adminStat:${adminList}" th:switch="${myList.name}" th:class="${adminStat.even}?'success':'info'">
<td th:text="${myList.id}"></td>
<td th:text="${myList.name}"></td>
<td th:text="${myList.password}"></td>
<td th:case="wpzhang">wpzhang likes runnning</td>
<td th:case="pzhang">pzhang is a shy girl</td>
<td th:case="xlyu">xlyu is outgoing</td>
<td th:case="jysu">jysu is a quiet boy</td>
<td th:text="${adminStat.index}"></td>
<td th:text="${adminStat.size}"></td>
<td th:text="${adminStat.first}"></td>
<td th:text="${adminStat.last}"></td>
<td th:if="${myList.name=='pzhang'}">girl</td>
<td th:unless="${myList.name=='pzhang'}">boy</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
解释:
adminStat:是状态变量,其属性有:
- index(从0开始取值)
- count(从1开始取值)
- size(对象的属性个数)
- current(当前迭代变量)
- even/odd(奇行还是偶行)
- first(是否是循环第一个),last同理