我想将字符串列表发布到我的控制器。
但它总是只取第一个选择的值。
我的百里香 html 表单:
<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>
我的控制器:
@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";
}
我认为你必须注释权限
@RequestParam("privileges")
它不是 ModelAttribute,但您从请求中收到它
编辑:两个 SO 线程可以更好地理解之间的区别@RequestParam https://stackoverflow.com/questions/29370581/spring-mvc-please-explain-difference-between-requestparam-and-modelattribute and @ModelAttribute https://stackoverflow.com/questions/27021679/why-not-use-requestparam-instead-of-modelattribute.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)