1. Overview
1.概述
In this tutorial, we’re going to have a look at the different types of conditionals available in Thymeleaf.
在本教程中,我们将看看Thymeleaf中可用的不同类型的条件语句。
For a quick introduction to Thymeleaf, please refer to this article.
关于Thymeleaf的快速介绍,请参考本文文章。
2. Maven Dependencies
2.Maven的依赖性
Let’s start with the Maven dependencies that are required to use Thymeleaf along with Spring:
让我们从使用Thymeleaf和Spring所需的Maven依赖项开始。
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.11.RELEASE</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
<version>3.0.11.RELEASE</version>
</dependency>
For other Spring releases, we should use the matching thymeleaf-springX library, where X stands for Spring version. Please also note that Spring 5 is supported by Thymeleaf starting with 3.0.8.RELEASE.
对于其他Spring版本,我们应该使用匹配的thymeleaf-springX库,其中X代表Spring版本。请注意,Thymeleaf从3.0.8.RELEASE开始支持Spring 5。
The latest versions of required dependencies can be found here.
所需依赖的最新版本可以在这里找到。
3. Thymeleaf Conditionals
3.百里香的条件语
We have to distinguish between conditionals that allow us to render text within an HTML element depending on a condition and those that control the instantiation of an HTML element itself.
我们必须区分允许我们根据某个条件渲染HTML元素中的文本的条件语和控制HTML元素本身实例化的条件语。
Let’s define our Teacher model class that we’ll use throughout this article:
让我们来定义我们的教师模型类,我们将在本文中使用这个模型。
public class Teacher implements Serializable {
private String gender;
private boolean isActive;
private List<String> courses = new ArrayList<>();
private String additionalSkills;
3.1. Elvis Operator
3.1.猫王运营商
The Elvis operator ?: lets us render text within an HTML element depending on the current state of a variable.
猫王操作符?让我们根据一个变量的当前状态在一个HTML元素中渲染文本。
We can use default expressions to provide a default text if a variable is null:
如果一个变量是null,我们可以使用默认表达式来提供一个默认文本。
<td th:text="${teacher.additionalSkills} ?: 'UNKNOWN'" />
Here we want to display the content of the teacher.additionalSkills variable if it is defined, and we want the text “UNKNOWN” to be rendered otherwise.
这里我们想显示teacher.extraSkills变量的内容,如果它被定义了,我们想让文本”UNKNOWN“被呈现出来。
It’s also possible to display arbitrary text depending on a boolean expression:
也可以根据布尔表达式来显示任意的文本。
<td th:text="${teacher.active} ? 'ACTIVE' : 'RETIRED'" />
We can query a simple boolean variable as in the previous example, but string comparisons and range checks are possible as well.
我们可以像前面的例子那样查询一个简单的布尔变量,但是字符串比较和范围检查也是可能的。
The following comparators and their textual representations are supported: > (gt), >= (ge), < (lt), <= (le), == (eq) and != (ne).
支持下列比较器及其文字表述。> (gt), >= (ge), < (lt), <= (le), == (eq) 和 !
3.2. If – Unless
3.2.如果 – 除非
The th:if and th:unless attributes allow us to render an HTML element depending on a provided condition:
th:if和th:unless属性允许我们根据提供的条件来渲染一个HTML元素。
<td>
<span th:if="${teacher.gender == 'F'}">Female</span>
<span th:unless="${teacher.gender == 'F'}">Male</span>
</td>
If the content of the teacher.gender variable equals to an F, the span element with the value Female is rendered. Otherwise, the element with Male is rendered.
如果teacher.gender变量的内容等于F,则呈现值为Female的span元素。否则,会呈现出带有Male的元素。
Such a setup is comparable to an if-else clause present in most programming languages.
这样的设置与大多数编程语言中的if-else 句子相当。
3.3. Switch – Case
3.3.开关 – 案例
If there are more than two possible results of an expression, we can use the th:switch and th:case attributes for the conditional rendering of the HTML elements:
如果一个表达式有两个以上的可能结果,我们可以使用th:switch和th:case属性来对HTML元素进行条件渲染。
<td th:switch="${#lists.size(teacher.courses)}">
<span th:case="'0'">NO COURSES YET!</span>
<span th:case="'1'" th:text="${teacher.courses[0]}"></span>
<div th:case="*">
<div th:each="course:${teacher.courses}" th:text="${course}"/>
</div>
</td>
Depending on the size of the teacher.courses list, we either display a default text, the single course or all courses available. We use the asterisk (*) for the default option.
根据teacher.courses列表的大小,我们要么显示一个默认文本,要么显示单个课程或所有可用的课程。我们用星号(*)表示默认选项。
4. Conclusion
4.总结
In this short article, we investigated the different types of Thymeleaf conditionals and presented some simplified examples showing the various options.
在这篇短文中,我们研究了不同类型的Thymeleaf条件语,并介绍了一些简化的例子,展示了各种选项。
The examples can be found in the GitHub project.
这些例子可以在GitHub项目中找到。