Java Thymeleaf下拉菜单
我正在尝试在我的表单中创建一个下拉列表。我的用户输入的文本输入显示出来,但是,我的枚举下拉列表没有列出这些值。我知道这里还有其他关于同一主题的帖子(我已经读过),但似乎仍然无法显示下拉列表。有人能帮我吗?这就是我尝试过的Java Thymeleaf下拉菜单,java,spring-boot,enums,thymeleaf,Java,Spring Boot,Enums,Thymeleaf,我正在尝试在我的表单中创建一个下拉列表。我的用户输入的文本输入显示出来,但是,我的枚举下拉列表没有列出这些值。我知道这里还有其他关于同一主题的帖子(我已经读过),但似乎仍然无法显示下拉列表。有人能帮我吗?这就是我尝试过的 <form action="#" th:action="@{${isAdded}?'/save':'/update'}" th:object="${user}" method="post" enctype="multipart/form-data"
<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...
这是我的枚举用户类型
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>
它不起作用。我将添加刚才在上述代码示例中尝试的内容。好的-明白。你能帮我澄清一下“它不起作用”吗?呈现什么HTML?是否有任何错误消息?首先,感谢您的帮助。在上面HTML的第三部分中,我可以显示下拉列表,但下拉列表中没有任何值。我想知道th:value或th:text值是否编码错误?在这一点上,我没有收到任何使故障排除变得困难的错误消息@安德烈·詹姆斯
@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>