【问题标题】:Thymeleaf Dropdown MenuThymeleaf 下拉菜单
【发布时间】:2020-09-02 17:14:04
【问题描述】:

我正在尝试在我的表单中创建一个下拉列表。我的用户输入的文本输入出现了,但是,我的枚举下拉列表没有列出值。我知道这里还有其他关于同一主题的帖子(我已经阅读过),但似乎仍然无法显示下拉菜单。有人能帮我吗?这是我尝试过的......

            <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...

我也尝试过基于这个StackOverflow Post...How to display all possible enum values in a dropdown list using Spring and Thymeleaf?

 <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>

这是我的用户类。

@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;

这是我的枚举 UserType...

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

【问题讨论】:

  • 它没有用。我将在上面的代码示例中添加我尝试过的内容。
  • 好的 - 明白了。您能否帮助澄清“它不起作用”?呈现什么 HTML?是否有任何错误消息?
  • 首先,感谢您的帮助。在上面的第三位 HTML 中,我可以显示下拉列表,但下拉列表中没有任何值。我想知道 th:value 或 th:text 值是否编码不正确?在这一点上,我没有收到任何导致故障排除的错误消息。 @andrewjames

标签: java spring-boot enums thymeleaf


【解决方案1】:

我找到了解决办法

 <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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-08
    • 1970-01-01
    • 2017-10-06
    • 2018-01-27
    • 1970-01-01
    • 2015-08-08
    • 2021-10-14
    相关资源
    最近更新 更多