1. Introduction
1.绪论
In this quick tutorial, we’re going to learn how to work with enums in Thymeleaf.
在这个快速教程中,我们将学习如何在Thymeleaf中使用枚举。
We’ll start by listing enum values in a dropdown. After that, we’ll look at using our enum for flow control within our templates.
我们将首先在一个下拉菜单中列出枚举值。之后,我们将研究如何在我们的模板中使用我们的枚举进行流程控制。
2. Setup
2.设置
Let’s start by adding the Spring Boot starter for Thymeleaf to our pom.xml file:
让我们首先将Spring Boot starter for Thymeleaf添加到我们的pom.xml文件。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<versionId>2.2.2.RELEASE</versionId>
</dependency>
We’re going to be working with widgets that have a few choices of color, so let’s define our Color enum:
我们将与有一些颜色选择的部件一起工作,所以让我们定义我们的Color枚举。
public enum Color {
BLACK, BLUE, RED, YELLOW, GREEN, ORANGE, PURPLE, WHITE
}
Now, let’s create our Widget class:
现在,让我们创建我们的Widget类。
public class Widget {
private String name;
private Color color;
// Standard getters/setters
}
3. Displaying Enums in a Dropdown Menu
3.在下拉菜单中显示枚举
Let’s use select and option to create a dropdown that uses our Color enum:
让我们使用select and option来创建一个使用我们的Color枚举的下拉菜单。
<select name="color">
<option th:each="colorOpt : ${T(com.baeldung.thymeleaf.model.Color).values()}"
th:value="${colorOpt}" th:text="${colorOpt}"></option>
</select>
The T operator is part of the Spring Expression Language for specifying an instance of a class or accessing static methods.
T操作符是Spring表达式语言的一部分,用于指定一个类的实例或访问静态方法。
If we run our application and navigate to our widget entry page, we’ll see all our colors in the Color dropdown:
如果我们运行我们的应用程序并导航到我们的小组件条目页面,我们会在Color下拉菜单中看到我们所有的颜色。
When we submit our form, our Widget object will be populated with the selected color:
当我们提交表格时,我们的Widget对象将被填充为所选颜色:。
4. Using a Display Name
4.使用显示名称
Since all capital letters can be a bit jarring, let’s expand on our example to provide more user-friendly dropdown labels.
由于所有的大写字母可能有点刺眼,让我们在我们的例子上进行扩展,以提供更方便用户的下拉标签。
We’ll start by modifying our Color enum to provide a display name:
我们将首先修改我们的Color枚举,以提供一个显示名称。
public enum Color {
BLACK("Black"),
BLUE("Blue"),
RED("Red"),
YELLOW("Yellow"),
GREEN("Green"),
ORANGE("Orange"),
PURPLE("Purple"),
WHITE("White");
private final String displayValue;
private Color(String displayValue) {
this.displayValue = displayValue;
}
public String getDisplayValue() {
return displayValue;
}
}
Next, let’s head over to our Thymeleaf template and update our dropdown to use the new displayValue:
接下来,让我们到我们的Thymeleaf模板,更新我们的下拉菜单以使用新的displayValue。
<select name="color">
<option th:each="colorOpt : ${T(com.baeldung.thymeleaf.model.Color).values()}"
th:value="${colorOpt}" th:text="${colorOpt.displayValue}"></option>
</select>
Our dropdown now displays with the more readable color names:
我们的下拉菜单现在以更易读的颜色名称显示。
5. If Statements
5.如果声明
Sometimes we might want to vary what we’re displaying based on the values of an enum. We can use our Color enum with Thymeleaf conditional statements.
有时我们可能想根据一个枚举的值来改变我们所显示的内容。我们可以通过Thymeleaf条件语句使用我们的Color枚举。
Let’s imagine that we have opinions on some of the possible widget colors.
让我们想象一下,我们对一些可能的小部件颜色有意见。
We can use a Thymeleaf if statement with our Color enum to conditionally display text:
我们可以使用Thymeleaf if语句和我们的Color枚举来有条件地显示文本。
<div th:if="${widget.color == T(com.baeldung.thymeleaf.model.Color).RED}">
This color screams danger.
</div>
Another option is to use a String comparison:
另一个选择是使用String比较。
<div th:if="${widget.color.name() == 'GREEN'}">
Green is for go.
</div>
6. Switch-Case Statements
6.切换式语句
In addition to if statements, Thymeleaf supports a switch-case statement.
除了if语句之外,Thymeleaf还支持switch-case语句。
Let’s use a switch-case statement with our Color enum:
让我们用switch-case语句来处理我们的Color枚举。
<div th:switch="${widget.color}">
<span th:case="${T(com.baeldung.thymeleaf.model.Color).RED}"
style="color: red;">Alert</span>
<span th:case="${T(com.baeldung.thymeleaf.model.Color).ORANGE}"
style="color: orange;">Warning</span>
<span th:case="${T(com.baeldung.thymeleaf.model.Color).YELLOW}"
style="color: yellow;">Caution</span>
<span th:case="${T(com.baeldung.thymeleaf.model.Color).GREEN}"
style="color: green;">All Good</span>
</div>
If we enter an orange widget, we should see our warning statement:
如果我们输入一个橙色的小部件,我们应该看到我们的警告声明。
7. Conclusion
7.结语
In this tutorial, we started by using Thymeleaf to create a dropdown using the Color enum we defined in our application. From there, we learned how to make the drop down display values more readable.
在本教程中,我们首先使用Thymeleaf创建了一个使用我们在应用程序中定义的Color枚举的下拉。在此基础上,我们学习了如何使下拉菜单的显示值更具可读性。
After going over the input side with the dropdown, we moved on to the output side and learned how to work with enums in control structures. We used both if and switch-case statements to condition some elements based on our Color enum.
通过下拉菜单了解了输入端之后,我们转到输出端,学习如何在控制结构中使用枚举。 我们使用if和switch-case语句,根据我们的Color枚举对一些元素设置条件。
The full example is available over on GitHub.
完整的例子可在GitHub上找到。