Thymeleaf条件判断

简单的条件:“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:asyncth:autofocusth:autoplay
th:checkedth:controlsth:declare
th:defaultth:deferth:disabled
th:formnovalidateth:hiddenth:ismap
th:loopth:multipleth:novalidate
th:nowrapth:openth:pubdate
th:readonlyth:requiredth:reversed
th:scopedth:seamlessth:selected

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值