带有 Thymeleaf 帖子列表的 Spring Boot [英] Spring Boot with Thymeleaf post list
问题描述
我想将字符串列表发布到我的控制器.但它总是只取第一个选定的值.
我的百里香 html 表单:
<div class="form-group"><label for="rolePrivileges">权限</label><select class="form-control" id="rolePrivileges" name="privileges" multiple="multiple" size="10" required="required"><option th:each="type : ${privilegesList}" th:value="${type}" th:text="${type}">Privilege</option></选择>
<button type="submit" class="btn btn-default">创建</button></表单>
我的控制器:
@RequestMapping(value = "/add", method = RequestMethod.POST)public String addSomething(Model model, @ModelAttribute("id") String id,@ModelAttribute("privileges") List特权){//用服务添加一些东西返回重定向:/角色";}
我认为你必须用
注释特权@RequestParam("权限")
它不是 ModelAttribute,而是您从请求中收到的
两个 SO 线程以更好地理解 @RequestParam 和 @ModelAttribute.
I want to post a list of strings to my controller. But it always only take the first selected value.
my thymeleaf html form:
<form action="/add" method="post">
<div class="form-group">
<label for="roleId">ID</label> <input type="text" class="form-control" id="roleId" name="id" required="required" />
</div>
<div class="form-group">
<label for="rolePrivileges">Privileges</label>
<select class="form-control" id="rolePrivileges" name="privileges" multiple="multiple" size="10" required="required">
<option th:each="type : ${privilegesList}" th:value="${type}" th:text="${type}">Privilege</option>
</select>
</div>
<button type="submit" class="btn btn-default">Create</button>
</form>
my controller:
@RequestMapping(value = "/add", method = RequestMethod.POST)
public String addSomething(Model model, @ModelAttribute("id") String id,
@ModelAttribute("privileges") List<String> privileges) {
// add something with a service
return "redirect:/roles";
}
I think you have to Annotate the privilges with
@RequestParam("privileges")
It is not a ModelAttribute but you receive it from the request
Edit: two SO threads to better understand the difference between @RequestParam and @ModelAttribute.
这篇关于带有 Thymeleaf 帖子列表的 Spring Boot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!