Site icon SpringHow

Enums in Thymeleaf templates

In this post, We will take a look at how we can use enums in thymeleaf with examples.

Select tag in HTML is usually a form input with a set of pre-defined options. Even though it is common to hard code these values, thymeleaf can offer you much more leverage with Enum support. Let’s see how we can do that.

Enum Setup

An Enum in java is a data type that allows us to define a set of predefined constants. We can define an enum as shown below for a list of roles for our demonstration.

public enum Role {
    GUEST,
    USER,
    ADMIN
}Code language: PHP (php)

Let’s use this Enum to create a dropdown in thymeleaf view.

Enums in a thymeleaf expression

In thymeleaf expressions, classes are referred to as T(…). Enum also follows the same rules. To use Enum in an expression, You need to wrap the fully qualified name of the enum. Here is an example.

<span th:text="${T(com.springhow.examples.springboot.thymeleaf.crud.domain.entities.Role).ADMIN}"></span>Code language: HTML, XML (xml)

This snippet results in the following.

<span>ADMIN</span>Code language: HTML, XML (xml)

Now that you know how to use enum in an expression, let’s see a couple of situations where enums may come handy.

Select element using Enum

As you can call any java methods within thymeleaf expressions, It is easier to get all the enum values for Role using the Role.values() method as shown below. As the values() method return an array, It is really easy to iterate through then using th:each as shown below.

<select name="role">
    <option th:each="role : ${T(com.springhow.examples.springboot.thymeleaf.crud.domain.entities.Role).values()}"
            th:text="${role}"
            th:value="${role}">
    </option>
</select>Code language: HTML, XML (xml)

This snippet will result in an HTML that would look like this.

<select name="role">
    <option value="GUEST">GUEST</option>
    <option value="USER">USER</option>
    <option value="ADMIN">ADMIN</option>
</select>Code language: HTML, XML (xml)

From now on you don’t need to manually change the dropdown content everywhere. Update it in the Enum definition, and it will reflect on every thymeleaf templates automatically.

Note that the Enum is referred with its fully qualified class name(i.e. with package name).

Customizing enum text

In the above example, the dropdown is all in Capital letters, and it may look unpleasant. In these cases, you can make leverage of enum constructors to create variants. For example, we have added a display text to our enum.

public enum Role {
    GUEST("Guest"),
    USER("User"),
    ADMIN("Admin");

    private final String displayText;

    Role(String displayText) {
        this.displayText = displayText;
    }

    public String getDisplayText() {
        return displayText;
    }
}Code language: JavaScript (javascript)
<select name="role">
    <option
            th:each="role : ${T(com.springhow.examples.springboot.thymeleaf.crud.domain.entities.Role).values()}"
            th:text="${role.displayText}"
            th:value="${role.name()}">
    </option>
</select>Code language: HTML, XML (xml)

If you have done this correctly, the dropdowns will have the following or similar output.

Enums with If statements

Thymeleaf treats enum as objects, thus its is straightforward to use them in expressions. You can do this in multiple different ways as we have shown here.

<a th:if="${userInfo.role.equals(T(com.springhow.examples.springboot.thymeleaf.crud.domain.entities.Role).ADMIN)}"
   href="/reports">
    View Reports
</a>
<a th:if="${userInfo.role == T(com.springhow.examples.springboot.thymeleaf.crud.domain.entities.Role).ADMIN}"
   href="/reports">
    View Reports
</a>
<a th:if="${userInfo.role.name().equals('ADMIN')}" href="/reports">
    View Reports
</a>Code language: HTML, XML (xml)

Enums in Switch cases

Enums are best with thymeleaf switch case rendering. Let’s use it with an example.

<div th:switch="userInfo.role">
    <div th:case="${T(com.springhow.examples.springboot.thymeleaf.crud.domain.entities.Role).GUEST}">Hello Guest</div>
    <div th:case="${T(com.springhow.examples.springboot.thymeleaf.crud.domain.entities.Role).USER}">Hello User</div>
    <div th:case="${T(com.springhow.examples.springboot.thymeleaf.crud.domain.entities.Role).ADMIN}">Hello Admin</div>
</div>Code language: HTML, XML (xml)

You can find the code for these examples at This GitHub Repository.

You should also consider reading the following posts relevant to this topic.

Exit mobile version