我正在尝试在表单中创建一个下拉列表。用户输入的文本输入正在显示,但是我的枚举下拉列表未列出这些值。我知道这里还有关于同一主题的其他帖子(我已经阅读过),但似乎仍然无法显示下拉列表。有人能帮我吗?这就是我尝试过的...
<form action="#" th:action="@{${isAdded}?'/save':'/update'}" th:object="${user}" method="post" enctype="multipart/form-data">
Dropdown for User type or role
<div class="form-group" >
<select th:field="*{type}">
<option
th:each="type : ${UserType.values()}"
th:value="${type}"
th:text="${type}">
</option>
</select>
</div>
<div class="form-group">
<input type="text", class="form-control" id="firstName" placeholder="First Name" th:field="*{firstName}">
</div>
.... The remaining text inputs have been omitted...
<div class="form-group" >
<select th:field="*{type}">
<option
th:each="type : ${T(com.abbyhowe.LearnFolio.models.User.type).values()}"
th:value="${type}"
th:text="${type}">
</option>
</select>
</div>
这将显示下拉列表,但没有将类型列表显示为可选选项。
<div class="form-group" >
<select th:field="*{type}">
<option
th:each="type : ${types}"
th:value="${type}"
th:text="${type}">
</option>
</select>
</div>
这是我的User类。
@Entity
@Table(name="user")
public class User implements Serializable {
/***
*
*/
private static final long serialVersionUID = -8885466378515990394L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Long id;
@NotBlank(message = "First name is required")
@Size(min = 2, max = 50, message = "Name must be between 3 and 50 characters")
@Column(name = "first_name")
private String firstName;
@Column(name = "type")
private UserType type;
这是我的枚举用户类型...
public enum UserType {
TEACHER("Teacher"),
STUDENT("Student"),
ADMIN("Account Administrator");
private final String displayName;
UserType(String displayName) {
this.displayName = displayName;
}
public String getDisplayName() {
return displayName;
}
}
我找到了解决方案
<form action="#" th:action="@{${isAdd}?'/save':'/update'}" th:object="${user}" method="post" enctype="multipart/form-data">
<!-- Dropdown for User type or role-->
<div class="form-group" >
<select th:field="*{type}">
<option
th:each="type : ${T(com.abbyhowe.LearnFolio.models.UserType).values()}"
th:value="${type}"
th:text="${type.displayName}">
</option>
</select>
</div>