一.簡單的條件:“if”和“unless”
th:if用法實例:
<table> <tr> <th>NAME</th> <th>PRICE</th> <th>IN STOCK</th> <th>COMMENTS</th> </tr> <tr th:each="prod : ${prods}" th:class="${prodStat.odd}? 'odd'"> <td th:text="${prod.name}">Onions</td> <td th:text="${prod.price}">2.41</td> <td th:text="${prod.inStock}? #{true} : #{false}">yes</td> <td> <span th:text="${#lists.size(prod.comments)}">2</span> comment/s <a href="comments.html" th:href="@{/product/comments(prodId=${prod.id})}" th:if="${not #lists.isEmpty(prod.comments)}">view</a> </td> </tr> </table>
如果值不是空:
1.如果一個布爾值,是true。
2.如果值是一個數字,非零 non-zero
3.如果是一個字符,非零值 non-zero
4.如果值是一個字符串,而不是“false”,“off” or “no”
5.如果值不是布爾,一個數字,一個字符或字符串。
(如果值是null,th:if將評估為false)。
th:unless用法:
<a href="comments.html" th:href="@{/comments(prodId=${prod.id})}" th:unless="${#lists.isEmpty(prod.comments)}">view</a>
二.switch用法:(th:switch
/ th:case)
<div th:switch="${user.role}"> <p th:case="'admin'">User is an administrator</p> <p th:case="#{roles.manager}">User is a manager</p> </div>
被指定為默認選項用th:case="*";相當於default,例如:
<div th:switch="${user.role}"> <p th:case="'admin'">User is an administrator</p> <p th:case="#{roles.manager}">User is a manager</p> <p th:case="*">User is some other thing</p> </div>