thymeleaf if怎么好用

2025-05-21 14:08:31
推荐回答(1个)
回答1:

简单的条件:“if”和“unless”
th:if用法实例:














NAMEPRICEIN STOCKCOMMENTS
Onions2.41yes
2 comment/s
th:href="@{/product/comments(prodId=${prod.id})}"
th:if="${not #lists.isEmpty(prod.comments)}">view



如果值不是空:   
1.如果一个布尔值,是true。  
2.如果值是一个数字,非零 non-zero 
3.如果是一个字符,非零值 non-zero 
4.如果值是一个字符串,而不是“false”,“off” or “no”  
5.如果值不是布尔,一个数字,一个字符或字符串。  
(如果值是null,th:if将评估为false)。

th:unless用法:
th:href="@{/comments(prodId=${prod.id})}"
th:unless="${#lists.isEmpty(prod.comments)}">view


二.switch用法:(th:switch / th:case)

User is an administrator


User is a manager




被指定为默认选项用th:case="*";相当于default,例如:

User is an administrator


User is a manager


User is some other thing