th:action 定義后台控制器路徑,類似<form>標簽的action屬性。
<form id="login-form" th:action="@{/login}">...</form>
th:each 對象遍歷,功能類似jstl中的<c:forEach>標簽。
<form id="login-form" th:action="@{/addStudent}" th:object="${stuReqBean}" method="POST">
<div class="student" th:each="stuIter,rowStat:${stuReqBean.students}">
<input type="text" class="firstName" value="" th:field="*{students[__${rowStat.index}__].firstName}"/>
...
</div></form>
上面的例子中通過選擇表達式*{}既能將表單綁定到后台的StudentRequestBean中的集合屬性students,也能將Servlet上下文中的StudentRequestBean中的List類型的students變量回顯,回顯時通過th:each進行遍歷。
注意1:綁定集合屬性元素下標的用法*{students[__${rowStat.index}__].firstName}
注意2:如果List<Student> students為null,頁面將無法顯示表單,后台必須給students初始化一個值
注意3:stuIter代表students的迭代器
th:field 常用於表單字段綁定。通常與th:object一起使用。 屬性綁定、集合綁定。
<form id="login-form" th:action="@{/login}" th:object="${loginBean}">...
<input type="text" value="" th:field="*{username}"></input>
<input type="text" value="" th:field="*{user[0].username}"></input>
</form>
th:href 定義超鏈接,類似<a>標簽的href 屬性。value形式為@{/logout}
<a th:href="@{/logout}" class="signOut"></a>
th:id div id聲明,類似html標簽中的id屬性。
<div class="student" th:id = "stu+(${rowStat.index}+1)"></div>
th:if 條件判斷。
<div th:if="${rowStat.index} == 0">... do something ...</div>
th:fragment 我們可以使用th:fragment屬性來定義一個模板,聲明定義該屬性的div為模板片段,常用與頭文件、頁尾文件的引入。常與th:include,th:replace一起使用。
例如:聲明模板片段/WEBINF/templates/footer. html
<div th: fragment=" copy" >
© 2011 The Good Thymes Virtual Grocery
</div>
引入模板片段
<div th: include=" /templates/footer : : copy" ></div>
<div th: replace=" /templates/footer : : copy" ></div>
th:include 加載模板的內容
見上
th:replace 替換當前標簽為模板的中標簽
見上
th:object 用於表單數據對象綁定,將表單綁定到后台controller的一個JavaBean參數。常與th:field一起使用進行表單數據綁定
<form id="login-form" th:action="@{/login}" th:object="${loginBean}">...</form>
th:src 用於外部資源引入,類似於<script>標簽的src屬性,常與@{}一起使用。
<script th:src="@{/resources/js/jquery/jquery.json-2.4.min.js}"
th:value 用於標簽復制,類似<option>標簽的value屬性。
<option th:value="Adult">Adult</option>
<input id="msg" type="hidden" th:value="${msg}" />
th:remove 刪除代碼
- all 刪除當前標簽和其內容和子標簽
- body 不刪除當前標簽,但是刪除其內容和子標簽
- tag 刪除當前標簽,但不刪除子標簽
- all-but-first 刪除除第一個子標簽外的其他子標簽
- none 啥也不干
<a href="/something" th:remove="${condition}? tag : none">Link text not to be removed</a>
<table> <tr th:remove="all"> <td>Mild Cinnamon</td> </tr> </table>