简体   繁体   English

如何使用 Spring 和 Thymeleaf 在下拉列表中显示所有可能的枚举值?

[英]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.我有一个具有 enum 属性的域对象,我想在该对象的表单中显示一个包含所有可能枚举值的下拉列表。 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";
}

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 :此外,如果要将枚举序号名称与 GUI 中显示的字符串分开,请添加其他属性,例如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:在 html 文件中:

<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.这将向用户显示displayName并允许您稍后静默更改此字符串而无需重构代码。 You may add more properties like th:title this way.您可以通过这种方式添加更多属性,例如th:title

this worked for me:这对我有用:

Java:爪哇:

public enum RoleEnum {
    SUPER_ADMIN("SUPER_ADMIN"),
    RESTAURANTE_ADMIN("RESTAURANTE_ADMIN");

    private final String roleCode;

    private RoleEnum(String roleCode) {
        this.roleCode = roleCode;
    }
}

Thymeleaf:百里香:

<select class="form-control" id="val-skill" name="role_id">
    <option th:each="role : ${T(com.users.enumeration.RoleEnum).values()}" th:value="${role}" th:text="${role}"></option>
</select>

for performance it is better to store the enums value in a field of the controller and send it as a model attribute because every call to enum.values generate a new copy of the values array.为了提高性能,最好将枚举值存储在控制器的一个字段中并将其作为模型属性发送,因为每次调用 enum.values 都会生成值数组的新副本。 depending on the size on the enum this can represent some work repeted.根据枚举的大小,这可以代表一些重复的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM