Thymeleaf - Displaying Enums in a Select Box - Dropdown Menu

In this article, we will discuss how to use Enums in a select box of Thymeleaf HTML template with an example.

Displaying Enums in a Dropdown Menu

Let's first define our Color enum class:
public enum Color {
    BLACK, BLUE, RED, YELLOW, GREEN, ORANGE, PURPLE, WHITE
}
Let's use select and option to create a dropdown that uses our Color enum:
<select name="color">
    <option th:each="colorOpt : ${T(com.example.thymeleaf.model.Color).values()}"
        th:value="${colorOpt}" th:text="${colorOpt}"></option>
</select>


Comments