Html 在Thymeleaf中创建下拉菜单和表单
我想创建一个下拉菜单,允许客户端通过下拉菜单中指定的字段搜索用户。例如,按州搜索、按城市搜索等 这就是我到目前为止所做的:Html 在Thymeleaf中创建下拉菜单和表单,html,spring,spring-mvc,thymeleaf,Html,Spring,Spring Mvc,Thymeleaf,我想创建一个下拉菜单,允许客户端通过下拉菜单中指定的字段搜索用户。例如,按州搜索、按城市搜索等 这就是我到目前为止所做的: <p>Search options:</p> <form action="#" th:action="@{/get/{value}" method="get"> <select> <option th:value="AllUsers">Search all users</option&
<p>Search options:</p>
<form action="#" th:action="@{/get/{value}" method="get">
<select>
<option th:value="AllUsers">Search all users</option>
<option th:value="ByUsername">Search by user name</option>
<option th:value="ByFirstname">Search by first name</option>
<option th:value="ByLastname">Search by last name</option>
<option th:value="ByAddress">Search by address</option>
<option th:value="ByCity">Search by city</option>
<option th:value="ByState">Search by state</option>
<option th:value="ByZipCode">Search by zip code</option>
<option th:value="ByPhoneNumber">Search by phone number</option>
<option th:value="ByEmail">Search by email</option>
</select>
<input type="text" th:field="value" name="searchField"/>
<input type="submit" value="Search" name="searchButton"/>
</form>
您在该链接中仅获得了所需答案:
因为公认的答案不是使用Thymeleaf,而且这个问题在谷歌排名第一,所以我在这里添加了我的解决方案 在我的情况下,
状态
是枚举值的列表。模型属性的填充方式与Spring中的通常方式相同:
mav.addObject("statuses", EnumSet.allOf(Status.class));
组具有类型为Status(枚举)的字段
这将自动填充列表并选择在我的组实例中选择的值:
链接中的这篇文章使用JSP,而不是thymeleaf。这里的group是什么&group.status意味着什么?在thymeleaf中是否为group a关键字/答案中是:group有一个类型为Status的字段(enumyes,谢谢..通过此更改,我可以在UI上显示所选的值,但ENUM中的其余值没有填充?有没有办法?现在我的下拉列表只包含一个值(男性代表性别),我需要显示所有的男性值和女性值,并让用户能够在填充之前选择的值后更改它,这就是
mav.addObject(“Status”,EnumSet.allOf(Status.class));
的用途。mav
是ModelAndView对象。啊,懂了。Thx
mav.addObject("statuses", EnumSet.allOf(Status.class));
<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>