简单的条件:“if” 和“unless”:

<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>

当${not #lists.isEmpty(prod.comments)}为TRUE的时候。就显示超链接。否则不显示;

th:if不仅判断返回为true的表达式,还判断一些特殊的表达式。

  • 如果值非NULL得话返回true: 
    • 如果值是boolean类型并为TURE.
    • 如果值是数值型并不为0.
    • 如果值是字符型并不为空.
    • 如果值是字符型并且内容不为“false”, “off” 或者 “no”。
    • 如果值不是上述类型。
  • 如果值是NULL得话返回false;

th:unless是th:if的反义。

Switch 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>
<p th:case="*">User is some other thing</p>
</div>

还有一些简单判断:用运算表达式直接判断

<dl th:hidden="${shareData.score == 0}">
th:async th:autofocus th:autoplay
th:checked th:controls th:declare
th:default th:defer th:disabled
th:formnovalidate th:hidden th:ismap
th:loop th:multiple th:novalidate
th:nowrap th:open th:pubdate
th:readonly th:required th:reversed
th:scoped th:seamless th:selected

转载于:https://my.oschina.net/carmen001/blog/818009