thymeleaf - Thymeleaf 检查变量是否等于数字
问题描述
我有以下 Thymeleaf 代码,如果 taxHistory.landValue 等于 0,我希望打印“--”,如果 taxHistory.landValue 大于 0,则打印实际值
<td th:if"${taxHistory.landValue==0}" th:text="--"></td>
<td th:unless="${taxHistory.landValue>0}" th:text="|${taxHistory.landValue}|"></td>
但是我收到以下错误
org.thymeleaf.exceptions.TemplateProcessingException: Could not parse as expression: "0}"
在百里香中这样做的正确方法是什么?
解决方案
有多种方法可以做到这一点,但如果您想要的条件不匹配,您可以使用三元运算符并打印您的字符串:
<td th:text="${taxHistory.landValue > 0 ? taxHistory.landValue : '--' }">[taxHistory.landValue]</td>
您可以使用该>
符号来保持HTML 格式正确。这假设一个负值也会打印你的字符串(你没有提到那种不寻常的情况下的行为)。