在 Thymeleaf 中创建下拉菜单和表单
Creating drop down menu and form in Thymeleaf
我想创建一个下拉菜单,允许客户端通过下拉菜单中指定的字段搜索用户。比如按州搜索,按城市搜索等
这是我目前拥有的:
<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>
我只是不确定如何连接下拉列表中当前所选项目的action
和value
标记来指定URI。如果用户选择,按州搜索,输入"Maryland",如何指定对应的URI标签?
这将是我在 Spring 中执行操作的方法:
@RequestMapping(value = "/get/ByState", method = RequestMethod.GET)
public String getByState() {
// ...
}
@RequestMapping(value = "/get/ByCity", method = RequestMethod.GET)
public String getByCity() {
// ...
}
您在这个 link 中正好有所需的答案:
因为接受的答案没有使用 Thymeleaf,而且这个问题在 Google 中排在首位,所以我在此处添加我的解决方案。
在我的情况下 statues
是枚举值的列表。模型属性像往常一样填充 Spring:
mav.addObject("statuses", EnumSet.allOf(Status.class));
组有一个 Status 类型的文件(枚举)。
<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>
这会自动填充列表并选择在我的组实例中选择的值:
我想创建一个下拉菜单,允许客户端通过下拉菜单中指定的字段搜索用户。比如按州搜索,按城市搜索等
这是我目前拥有的:
<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>
我只是不确定如何连接下拉列表中当前所选项目的action
和value
标记来指定URI。如果用户选择,按州搜索,输入"Maryland",如何指定对应的URI标签?
这将是我在 Spring 中执行操作的方法:
@RequestMapping(value = "/get/ByState", method = RequestMethod.GET)
public String getByState() {
// ...
}
@RequestMapping(value = "/get/ByCity", method = RequestMethod.GET)
public String getByCity() {
// ...
}
您在这个 link 中正好有所需的答案:
因为接受的答案没有使用 Thymeleaf,而且这个问题在 Google 中排在首位,所以我在此处添加我的解决方案。
在我的情况下 statues
是枚举值的列表。模型属性像往常一样填充 Spring:
mav.addObject("statuses", EnumSet.allOf(Status.class));
组有一个 Status 类型的文件(枚举)。
<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>
这会自动填充列表并选择在我的组实例中选择的值: