码迷,mamicode.com
首页 > 其他好文 > 详细

Thymeleaf 条件语句

时间:2018-09-12 12:32:57      阅读:1371      评论:0      收藏:0      [点我收藏+]

标签:ext   语句   nbsp   nts   less   有一个   time   blank   ref   

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>

 

 
 
 
 

Thymeleaf 条件语句

标签:ext   语句   nbsp   nts   less   有一个   time   blank   ref   

原文地址:https://www.cnblogs.com/xiaohu1218/p/9634101.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!