Thymeleaf中使用if和unless实现状态的判断显示达到if-else逻辑判断的效果
生活随笔
收集整理的這篇文章主要介紹了
Thymeleaf中使用if和unless实现状态的判断显示达到if-else逻辑判断的效果
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
場景
Thymeleaf官方文檔:
https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#introducing-thymeleaf
官方文檔說明:
有時,只有在滿足特定條件時,才需要顯示模板的片段。
官方使用示例:
<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<ahref = "comments.html"<BR>????????th:href="@{/product/comments(prodId=${prod.id})}"th:if="${not #lists.isEmpty(prod.comments)}">view</a></td></tr> </table>注意:
th:if屬性不僅計算布爾型數據,還能計算表達式的值為true時也遵守如下規則:
1.如果值為NULL:
則th:if將計算為false
2.如果值不為NULL:
①值為布爾型,為true。
②值為數字,為非0。
③值為字符,為非0。
④值為字符串,不為“false”、"off"、“no”
還有,th:if有一個逆屬性,th:unless,除非。。。
官方舉例:
<a href="comments.html"th:href="@{/comments(prodId=${prod.id})}"th:unless="${#lists.isEmpty(prod.comments)}">view</a>實現
?
<div class="form-group col-md-4 ml_10"><label>業務物流類型:</label><input type="text" th:value="物料清潔上料" th:if="${detailsVO==null || detailsVO.orderType ==null ?'':detailsVO.orderType=='01'}" readonly="readonly" class="form-control"/><input type="text" th:value="托盤清潔上料" th:if="${detailsVO==null || detailsVO.orderType ==null ?'':detailsVO.orderType=='02'}" readonly="readonly" class="form-control"/><input type="text" th:value="托盤清潔上臺" th:if="${detailsVO==null || detailsVO.orderType ==null ?'':detailsVO.orderType=='03'}" readonly="readonly" class="form-control"/><input type="text" th:value="廢料包材出庫" th:if="${detailsVO==null || detailsVO.orderType ==null ?'':detailsVO.orderType=='04'}" readonly="readonly" class="form-control"/><input type="text" th:value="廢料托盤出庫" th:if="${detailsVO==null || detailsVO.orderType ==null ?'':detailsVO.orderType=='05'}" readonly="readonly" class="form-control"/><input type="text" th:value="不良品出庫" th:if="${detailsVO==null || detailsVO.orderType ==null ?'':detailsVO.orderType=='06'}" readonly="readonly" class="form-control"/><input type="text" th:value="清潔設備補盤" th:if="${detailsVO==null || detailsVO.orderType ==null ?'':detailsVO.orderType=='07'}" readonly="readonly" class="form-control"/><input type="text" th:value="待清潔入庫" th:if="${detailsVO==null || detailsVO.orderType ==null ?'':detailsVO.orderType=='08'}" readonly="readonly" class="form-control"/><input type="text" th:value="不良品暫估" th:if="${detailsVO==null || detailsVO.orderType ==null ?'':detailsVO.orderType=='09'}" readonly="readonly" class="form-control"/><input type="text" th:value="未知狀態" th:unless="${detailsVO==null || detailsVO.orderType ==null ?'':detailsVO.orderType=='01'||detailsVO.orderType=='02'||detailsVO.orderType=='03'||detailsVO.orderType=='04'||detailsVO.orderType=='05'||detailsVO.orderType=='06'||detailsVO.orderType=='07'||detailsVO.orderType=='08'||detailsVO.orderType=='09'}" readonly="readonly" class="form-control"/></div>
?
總結
以上是生活随笔為你收集整理的Thymeleaf中使用if和unless实现状态的判断显示达到if-else逻辑判断的效果的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SpringBoot+Thymeleaf
- 下一篇: Python2.7安装ncmbot时提示