1、th:action
定義后台控制器的路徑,類似<form>標簽的action屬性。 示例如下。
<form id="login" th:action="@{/login}">......</form>
2、th:each
對對象集合進行遍歷輸出,和jstl中的<c: forEach>類似,而且這個屬性不僅可以循環集合,還可以循環數組及Map,示例如下。
<ol> <li>List類型的循環: <table > <tr> <th>用戶名</th> <th>用戶郵箱</th> </tr> <tr th:each="user,iterStat : ${list}"> <td th:text="${user.userName}">張三</td> <td th:text="${user.email}">zhangsan@qq.com</td> </tr> </table> </li> <li>Map類型的循環: <div th:each="mapS:${map}"> <div th:text="${mapS}"></div> </div> </li> <li>數組的循環: <div th:each="arrayS:${arrays}"> <div th:text="${arrayS}"></div> </div> </li> </ol>
iterStat還有${iterStat.index}、${iterStat.count}等屬性,其屬性的含義為:
index:當前迭代對象的index(從0開始計算)
count: 當前迭代對象的index(從1開始計算)
size:被迭代對象的大小
current:當前迭代變量
even/odd:布爾值,當前循環是否是偶數/奇數(從0開始計算)
first:布爾值,當前循環是否是第一個
last:布爾值,當前循環是否是最后一個
3、th:href
定義超鏈接,類似<a>標簽的href 屬性。value形式為@{/login}
<a class="login" th:href="@{/login}"></a>
4、th:src
用於外部資源引入,類似<script>標簽的src屬性,常與@{}表達式結合使用。
<script th:src="@{/static/js/jquery-2.4.min.js}"></script>
5、th:id
類似html標簽中的id屬性。
<div class="user" th:id = "(${index})"></div>
6、th:if
用於進行判斷
<span th:if="${num} == 1" > <input type="redio" name="se" th:value="男" /> </span> <span th:if="${num} == 2"> <input type="redio" name="se" th:value="女" /> </span>
7、th:value
類似html中的value屬性,能對某元素的value屬性進行賦值。
<input type="hidden" id="vd" name="vd" th:value="${value}">
8、th:text
用於文本的顯示
<input type="text" id="username" name="username" th:text="${username}">
9、th:attr
用於給HTML中某元素的某屬性賦值。比如例子7還可以寫成如下形式.
<input type="hidden" id="vd" name="vd" th:attr="value=${value}" >
10、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>