Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display all possible enum values in a dropdown list using Spring and Thymeleaf?

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";
}
like image 876
stevecross Avatar asked Apr 08 '15 13:04

stevecross


People also ask

How do I get a list of all enum values?

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.

How do I get the enum value in Thymeleaf?

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.

Can you have a list of enums?

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.


2 Answers

You could do:

<select>
    <option th:each="state : ${T(com.mypackage.Ticket.State).values()}"
            th:value="${state}"
            th:text="${state}">
    </option>
</select>
like image 97
jchampemont Avatar answered Oct 01 '22 19:10

jchampemont


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.

like image 25
fkurth Avatar answered Oct 01 '22 21:10

fkurth