Thymeleaf is a popular server-side template engine for Java-based web and standalone environments. In this quick article, you'll learn how to use enum constant values in Thymeleaf templates.

We'll first look at how to display all enum values in a drop-down list and then how to compare enum constants in Thymeleaf. Let us say we have the following enum class named that represents several blog topics:


public enum Topic {

We have another model class called that stores a reference to the enum class to present post topic:



public class Post implements Serializable {

    private int id;
    private String title;
    private String body;
    private Topic topic;

    // constructor, getters, and setters removed for brevity

Finally, we have the following controller method that handles the all GET requests at / endpoint and returns a new Post object stored in Model:

public String homePage(Model model) {

    // add a new post object
    model.addAttribute("post", new Post());

    return "index";

Displaying All Enum Constants

Let us now create index.html Thymeleaf template in the src/main/resources/templates/ folder that renders a HTML form to allow users to create a new post as shown below:


<!DOCTYPE html>
<html lang="en" xmlns:th="">
    <meta charset="UTF-8">
    <title>Spring Boot Enum Example</title>

    <link rel="stylesheet" href="">

<div class="container my-5">
    <div class="row">
        <div class="col-8 mx-auto">
            <h1>Create New Post</h1>
            <form th:action="@{/save-post}" th:object="${post}" method="post">
                <div class="form-row">
                    <div class="col-md-6">
                        <div class="form-group">
                            <label for="title">Title</label>
                            <input type="text" id="title" th:field="*{title}" class="form-control">
                    <div class="col-md-6">
                        <div class="form-group">
                            <label for="topic">Select Topic</label>
                            <select class="form-control" id="topic" th:field="*{topic}">
                                <option th:each="topic : ${T(}"
                                        th:value="${topic}" th:text="${topic}"></option>
                <div class="form-group">
                    <label for="body">Body</label>
                    <textarea id="body" th:field="*{body}" class="form-control"></textarea>
                <button type="submit" class="btn btn-primary btn-block">Submit</button>


As you can see above, we are generating a drop-down list with all topics available to select. The Topic enum class provides a special method values() that returns all enum constants as an array.

The special T operator is a part of SpEL (Spring Expression Language) for accessing a type and invoking static methods or getting static properties. We have used it to access all the values from our enum class Topic.

Now if you run the application, you will see the following HTML form rendered in the browser:

Thymeleaf Enum Dropdown

Comparing Enum Constants

You can easily compare enum constants using Thymeleaf conditionals to control what you want to display to the visitor.

Using th:if Attribute

The following example demonstrates how you can use the Thymeleaf th:if conditional attribute to conditionally display a text:

<th:block th:each="topic : ${T(}">
    <div th:if="${topic eq T(}">
        Hey, I am a web developer!

Alternatively, you can also use string literal for comparison as shown below:

<th:block th:each="topic : ${T(}">
    <div th:if="${ eq 'WEB'}">
        Hey, I am a web developer!

Using th:switch Attribute

Another way to compare enum constants in Thymeleaf is by using the th:switch attribute as shown below:

<div th:each="topic : ${T(}">
    <span th:text="${topic}"></span>
    <th:block th:switch="${topic}" th:with="Topic=${T(}">
        <span th:case="${Topic.TECH}">- Technology Tutorials</span>
        <span th:case="${Topic.DESIGN}">- Design Tutorials</span>
        <span th:case="${Topic.WEB}">- Web Development Tutorials</span>
        <span th:case="${Topic.STARTUP}">- Startups Tutorials</span>
        <span th:case="${Topic.DEVTOOLS}">- Develpoment Tools Tutorials</span>

Notice the local variable Topic we have declared above. So instead of writing T() expression for every case, we just defined it once and then use the local variable for all cases.


That's all folks. In this article, we have learned how to use Java enum constants in Thymeleaf to render a drop-down list as well as compare them using conditionals to display additional texts.

Read Next: How to use Thymeleaf in Spring Boot

✌️ Like this article? Follow @attacomsian on Twitter. You can also follow me on LinkedIn and DEV. Subscribe to RSS Feed.

👋 If you enjoy reading my articles and want to support me to continue creating free tutorials, ☕ Buy me a coffee (cost $5).