Thymeleaf 条件语句

Thymeleaf 条件语句

if 与 unless

假如现在有一个商品列表,当商品有评论时,显示view按钮,否则不显示。这时候就可以使用Thymeleaf的th:if标签来实现:

1
2
3
<a href="comments.html"
th:href="@{/product/comments(prodId=${prod.id})}"
th:if="${not #lists.isEmpty(prod.comments)}">view</a>

 

当prod.comments不为空时,页面将渲染出该<a>标签。

另外,th:if有一个反向属性th:unless,用于代替上面的not:

1
2
3
<a href="comments.html"
th:href="@{/comments(prodId=${prod.id})}"
th:unless="${#lists.isEmpty(prod.comments)}">view</a>

 

switch

Thymeleaf中的th:switch和其他语言的switch case语句差不多:

1
2
3
4
<div th:switch="${user.role}">
<p th:case="'admin'">User is an administrator</p>
<p th:case="'manager'">User is a manager</p>
</div>

 

th:case="*"表示默认选项,相当于default:

1
2
3
4
5
<div th:switch="${user.role}">
<p th:case="'admin'">User is an administrator</p>
<p th:case="'manager'">User is a manager</p>
<p th:case="*">User is some other thing</p>
</div>

 

 
 
 
 

转载于:https://www.cnblogs.com/xiaohu1218/p/9634101.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值