且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如果未选中,则在UI中显示带有默认值的枚举值

更新时间:2022-12-05 12:09:27

最简单的方法是使用 th:object ,如下所示:

Easiest is to use th:object in the form as shown below:

<form th:object="${user}">
    <div class="form-group">
        <label class="control-label required"></label>
        <select id="maritalStatus" class="form-control required" th:field="*{maritalStatus}">
            <option value=""></option>
            <option th:each="status : ${maritalStatuses}" th:value="${status}" 
                th:text="${status}"></option>
        </select>
    </div>
    <div class="form-group">
        <label class="control-label"></label>
        <select id="gender" class="form-control" th:field="*{gender}">
            <option value=""></option>
            <option th:each="e : ${genders}" th:value="${e.name}" 
                th:text="${e}"></option>
        </select>
    </div>
</form>