In this article, we are going to show how to use enums in Thymeleaf templates. Starting with listing all available items in dropdown components, then showing how to use localization messages with enums, and finishing with using enums in comparison statements.
2. Setup
We prepared a simple enum that represents the planets of our solar system:
To save submitted form we use Home class that will be our main command object:
As usual, we used Lombok library to get rid of setters, getters, constructors and toString() methods for the sake of readability.
2. Using enums in dropdowns
The Java enum objects contains a static method that returns all available items - it is a values() method. Let's use it to generate dropdown with all planets available to select:
The special T operator is a part of Spring EL (Expression Language). We will use it to access a static method values() available an Enum Planet. Additionally, we put a simple INPUT field where a user can provide a title.
Rendered form for this template will look like the following:
As you can see we have all planets in the rendered dropdown with the same names that are provided in our Enum.
What if we want to have different names for select options? We simply need to use locale messages functionality described in the next chapter.
3. Using local messages for enum values
To change default locale configuration in our Spring Boot application we need to add a special WebMvcConfigurer class:
From now we can change locale by providing additional request parameter lang to any internal URL:
?lang=EN - for English,
?lang=ES - for Spanish.
The main form looks a little bit different now. Options in dropdown menu have now message expressions th:text="#{${'planet.' + planet}}" that concatenate planet. text with enum value.
In the following table we've put together a message properites with a screens from rendered websites:
planet.MERCURY=Mercurio
planet.VENUS=Venus
planet.EARTH=Tierra
planet.MARS=Marte
planet.JUPITER=Júpiter
planet.SATURN=Saturno
planet.URANUS=Urano
planet.NEPTUNE=Neptuno
submit.button=Enviar
home.title = Título
home.planet = Planet
home.selected = Planeta seleccionado
4. Using enum in comparision statements
We can easily use an enum to control what we want to display on the resulted website. We can use our Planet with Thymeleaf condition statements.
4.1. If statement
We can use Thymeleaf if statement with our enum to conditionally display additional text, like in the below example:
We can also use string comparision:
4.2. Switch statement
Thymeleaf fully supports enums in switch-case statements.
Let's use our Planet enum to display the order of the planet from the sun:
5. Conclusion
In this article, we focused on using enum values in Thymeleaf templates. Thymeleaf allows us to list all available enum values, used them in comparison statements and as a source of the locale messages.
The example code used in this article is available in our GitHub repository.
{{ 'Comments (%count%)' | trans {count:count} }}
{{ 'Comments are closed.' | trans }}