Html Thymeleaf - 布尔运算符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18006941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 11:46:52  来源:igfitidea点击:

Thymeleaf - boolean operators

htmlbooleanoperatorsthymeleaf

提问by Vinicin

How can I use boolean operators like andor orusing Thymeleaf?

如何使用类似andor使用 Thymeleaf 的布尔运算符?

For instance, if I want to show the data from a table if only one of the conditions is true.

例如,如果我想在只有一个条件为真时显示表中的数据。

<tr th:if="firstCondition or secondCondition">
  <td th:text="${entity.attr1}"</td>
  <td th:text="${entity.attr2}">Default Value</td>
</tr>

回答by Nimchip

Boolean operators work just like that. You use 'or', 'and' instead of the normal java nomenclature. You can also shorten your ifs.

布尔运算符就是这样工作的。您使用“或”、“和”而不是普通的 Java 命名法。你也可以缩短你的ifs。

You can try this:

你可以试试这个:

<tr th:if="${violation.remainingDebt != 0 or violation.validity}">

You need to nest them up in the same curly brackets, independently if they are isolated considering the logical 'or' operation being tested.

您需要将它们嵌套在相同的大括号中,如果考虑到正在测试的逻辑“或”操作,它们是独立的,则需要独立地嵌套它们。

Be wary though! This will only show you the tr and it's child elements if the if passes as true.

不过要小心!如果 if 为真,这只会向您显示 tr 及其子元素。

回答by Lucky

Instead of using conditional operators &&and ||in the expression like we use in Java and Javascript, in Thymeleaf we use the text ANDand ORfor comparison.

与我们在 Java 和 Javascript中使用的条件运算符&&||表达式不同,在 Thymeleaf 中我们使用文本ANDOR进行比较。

ORcondition example:

OR条件示例:

<div th:if="${fruit.name} == Apple OR ${fruit.name} == Orange ">
     <!-- fruit's name is either Apple or Orange -->
</div>

ANDcondition example:

AND条件示例:

<div th:if="${user.role} == 'ADMIN' AND ${user.property} == 'SPECIAL' ">
     <!-- User is admin and has SPECIAL previleges -->
</div>