Working with Boolean in Thymeleaf – 在Thymeleaf中使用布尔法

最后修改: 2018年 4月 25日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.绪论

In this quick tutorial, we’re going to look at how to work with boolean values in Thymeleaf.

在这个快速教程中,我们将看看如何在Thymeleaf中处理布尔值。

Before we dive into the details, Thymeleaf basics can be found in this write-up.

在我们深入了解细节之前,可以在本篇文章中找到Thymeleaf的基础知识。

2. Evaluating Expressions as Booleans

2.将表达式作为布尔值进行评估

In Thymeleaf, any value can be evaluated to a boolean. We have a few values interpreted as false:

在Thymeleaf中,任何值都可以被评估为布尔值。我们有几个值被解释为false

  • null
  • the boolean value false
  • the number 0
  • the character \0
  • the strings “false”, “off”and “no”

Any other value is evaluated to true.

任何其他值都被评估为true

3. Using Booleans as a Rendering Conditions

3.使用布尔值作为渲染条件

To render an HTML element conditionally, we have two options: the th:if and the th:unless attributes.

要有条件地渲染一个HTML元素,我们有两个选择:th:ifth:unless属性。

Their effect is exactly the opposite – Thymeleaf will render an element with a th:if attribute only if the attribute’s value is true and with th:unless attribute only if its value is false:

它们的效果正好相反–Thymeleaf将渲染一个带有th:if属性的元素,只有当该属性的值为true时才会渲染,而带有th:unless属性的元素只有当其值为false:时才渲染。

<span th:if="${true}">will be rendered</span>
<span th:unless="${true}">won't be rendered</span>
<span th:if="${false}">won't be rendered</span>
<span th:unless="${false}">will be rendered</span>

4. Logical and Conditional Operators

4.逻辑和条件运算符

In addition, we can use the three classic logic operators in Thymeleaf:

此外,我们可以在Thymeleaf中使用三个经典的逻辑运算符。

  • and
  • or
  • negation with the keyword not or the “!” symbol

We can use these operators inside variable expressions or combining multiple variable expressions with them:

我们可以在变量表达式内使用这些运算符,或者用它们组合多个变量表达式:

<span th:if="${isRaining or isCold}">The weather is bad</span>
<span th:if="${isRaining} or ${isCold}">The weather is bad</span>
<span th:if="${isSunny and isWarm}">The weather is good</span>
<span th:if="${isSunny} and ${isWarm}">The weather is good</span>
<span th:if="${not isCold}">It's warm</span>
<span th:if="${!isCold}">It's warm</span>
<span th:if="not ${isCold}">It's warm</span>
<span th:if="!${isCold}">It's warm</span>

We can also use conditional operators: the if-then, if-then-else, and the default operators.

我们还可以使用条件运算符:if-thenif-then-else,以及默认运算符。

The if-then-else operator is the usual ternary, or ?: operator:

if-then-else操作符是通常的三元组,或?:操作符。

It's <span th:text="${isCold} ? 'cold' : 'warm'"></span>

Moreover, the if-then operator is the simplified version where we don’t have an else part:

此外,if-then操作符是简化版本,我们没有else部分。

<span th:text="${isRaining or isCold} ? 'The weather is bad'"></span>

The default operator returns the first operand if it’s not null and the second otherwise:

如果第一个操作数不是null ,则默认操作数返回第二个操作数。

<span th:text="'foo' ?: 'bar'"></span> <!-- foo -->
<span th:text="null ?: 'bar'"></span> <!-- bar -->
<span th:text="0 ?: 'bar'"></span> <!-- 0 -->
<span th:text="1 ?: 'bar'"></span> <!-- 1 -->

The default operator is also called the Elvis operator because of its strong resemblance to Elvis’ hairstyle.

默认运算符也被称为猫王运算符,因为它与猫王的发型非常相像。

Note, that the Elvis operator only does a null check, it doesn’t evaluate the first operand as boolean.

注意,猫王运算符只做null 检查,它不会将第一个操作数评估为布尔值。

5. #bools Utility Object

5.#bools 实用对象

The #bools is a utility object which is available in expressions by default and has some handy methods:

#bools是一个实用对象,在表达式中默认可用,并且有一些方便的方法。

  • #bools.isTrue(obj) returns whether the argument is evaluated to true
  • #bools.isFalse(obj) returns whether the argument is evaluated to false
  • #bools.xxxIsTrue(collection) converts the elements of the argument to booleans with #bools.isTrue() then collects them to the same type of collection
  • #bools.xxxIsFalse(collection) converts the elements of the argument to booleans with #bools.isFalse() then collects them to the same type of collection
  • #bools.xxxAnd(collection) returns true if all elements in the argument is evaluated to true
  • #bools.xxxOr(collection) returns true if any element in the argument is evaluated to true

In the methods above xxx can be either array, list or set, depending on the method’s argument (and return value in case of xxxIsTrue() and xxxIsFalse()).

在上面的方法中,xxx可以是arraylistset,取决于方法的参数(以及xxxIsTrue()xxxIsFalse()的返回值)。

6. Conclusion

6.结语

In this article, we’ve seen how Thymeleaf interprets values as booleans, also how we can render elements conditionally and work with boolean expressions.

在这篇文章中,我们看到了Thymeleaf是如何将数值解释为布尔值的,也看到了我们如何有条件地渲染元素和使用布尔表达式。

As usual, the code (with more examples) is available over on Github.

像往常一样,代码(包括更多的例子)可以在Github上获得。