0
点赞
收藏
分享

微信扫一扫

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>

 

举报

相关推荐

if-else结构的判断语句

0 条评论