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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29515366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 08:09:21  来源:igfitidea点击:

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

javaspringthymeleaf

提问by stevecross

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:

我有一个具有 enum 属性的域对象,我想在该对象的表单中显示一个包含所有可能枚举值的下拉列表。想象以下对象:

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";
}

采纳答案by jchampemont

You could do:

你可以这样做:

<select>
    <option th:each="state : ${T(com.mypackage.Ticket.State).values()}"
            th:value="${state}"
            th:text="${state}">
    </option>
</select>

回答by fkurth

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 displayNameto the user and allows you to silently change this strings later without refactoring the code. You may add more properties like th:titlethis way.

这将向用户显示displayName并允许您稍后静默更改此字符串而无需重构代码。您可以通过这种方式添加更多属性,例如th:title

回答by Diego Santa Cruz Mendezú

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>