Creating drop down menu and form in Thymeleaf

前端 未结 2 1175
攒了一身酷
攒了一身酷 2021-01-01 08:22

I want to create a drop down menu that allows a client to search users by a field specified in the drop down menu. For example, search by state, search by city, etc.

相关标签:
2条回答
  • 2021-01-01 08:41

    Because the accepted answer is not using Thymeleaf and this question is top in Google I'm adding my solution here.

    In my situation statues is a list of Enum values. Model attribute is populated as usually you do in Spring:

    mav.addObject("statuses", EnumSet.allOf(Status.class));
    

    Group has a filed of type Status (the enum).

    <div class="form-group row">
        <select class="form-control" id="status" name="status">
            <option th:each="stat : ${statuses}"
                    th:value="${stat}"
                    th:text="${stat}"
                    th:selected="${stat.equals(group.status)}"/>
        </select>
    </div>
    

    This automatically populates the list and selects value that is selected in my Group instance:

    0 讨论(0)
  • 2021-01-01 08:42

    You have just the required answer in this link:

    http://fruzenshtein.com/spring-mvc-form-select-tag/

    0 讨论(0)
提交回复
热议问题