I have a domain object that has an enum property and I want to display a dropdown list with all possible enum values in the form for this object. Imagine the following object:
public class Ticket {
private Long id;
private String title;
private State state;
// Getters & setters
public static enum State {
OPEN, IN_WORK, FINISHED
}
}
In my controller I have a method that renders a form for this object:
@RequestMapping("/tickets/new")
public String showNewTicketForm(@ModelAttribute Ticket ticket) {
return "tickets/new";
}
The template looks like this:
<form th:action="@{/tickets}" method="post" th:object="${ticket}">
<input type="text" th:field="*{title}" />
<select></select>
</form>
Later it should be transformed to something like this:
<form action="/tickets" method="post">
<input type="text" name="title" />
<select name="state">
<option>OPEN</option>
<option>IN_WORK</option>
<option>FINISHED</option>
</select>
</form>
How can I create the select tag? The selected value should also be mapped to the ticket automatically so that I can do something like this in the controller:
@RequestMapping(value = "/tickets", method = RequestMethod.POST)
public String createTicket(@Valid Ticket ticket) {
service.createTicket(ticket);
return "redirect:/tickets";
}
The idea is to use the Enum. GetValues() method to get an array of the enum constants' values. To get an IEnumerable<T> of all the values in the enum, call Cast<T>() on the array. To get a list, call ToList() after casting.
To display enum values in the dropdown component we will use magic T to access enum and values() method to retrieve items. Additionally to display different text in option tags we use ${country. displayName} expression to display the inner enum field.
In Java 10 and later, you can conveniently create a non-modifiable List by passing the EnumSet . The order of the new list will be in the iterator order of the EnumSet . The iterator order of an EnumSet is the order in which the element objects of the enum were defined on that enum.
You could do:
<select>
<option th:each="state : ${T(com.mypackage.Ticket.State).values()}"
th:value="${state}"
th:text="${state}">
</option>
</select>
In addition, if you want to separate the enum ordinal name from the string displayed in the GUI, add additional properties, for example a displayName:
public static enum State {
OPEN("open"),
IN_WORK("in work"),
FINISHED("finished");
private final String displayName;
State(String displayName) {
this.displayName = displayName;
}
public String getDisplayName() {
return displayName;
}
}
And in the html file:
<select>
<option th:each="state : ${T(com.mypackage.Ticket.State).values()}" th:value="${state}" th:text="${state.displayName}"></option>
</select>
This will present the displayName to the user and allows you to silently change this strings later without refactoring the code. You may add more properties like th:title this way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With