[英]ModelAttribute adds only 1st value into List
On my view, I generate input "username", select with attribute multiple=multiple, which has name "rolesss". 在我看来,我生成输入“用户名”,并选择属性multi = multiple,其名称为“ rolesss”。
My issue is, that if I send such form via post, my controller should convert roles to list, but i get only list containing single element. 我的问题是,如果我通过邮寄发送此类表格,我的控制器应将角色转换为列表,但我只会得到包含单个元素的列表。
For example: I send post, with values: 例如:我发送带有以下值的帖子:
username:MyUser
_csrf:aef50238-92cf-48df-86a4-cb6e2b8f62c9
rolesss:USER
rolesss:ADMIN
In debug mode in my controller I see values: roless: "USER" username: "MyUser" 在控制器的调试模式下,我看到以下值:角色:“ USER”用户名:“ MyUser”
"ADMIN" did just disappear. “ ADMIN”并没有消失。
My controller looks like: 我的控制器看起来像:
@Controller
@RequestMapping("/user-management")
public class UserManagementController {
@RequestMapping("")
public ModelAndView home() {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("pages/user-management");
return modelAndView;
}
@RequestMapping(value = "", method = RequestMethod.POST)
public ModelAndView changeRoles(@ModelAttribute("username") String username,@ModelAttribute("rolesss") List<String> rolesss) {
return null;
}
}
My view I merged 2 thymeleaf fragments into 1, in my code #user-roles-form is in separate fragment, but I think that it should not change anything: 我的观点是我将2个百里香叶片段合并为1个,在我的代码#user-roles-form中是单独的片段,但是我认为它不应更改任何内容:
<th:block layout:fragment="main-content">
<section>
<h2 class="section-title no-margin-top">Role Management</h2>
<div class="form-group">
<div class="panel panel-primary">
<div class="panel-heading">Změna rolí uživatele</div>
<div class="panel-body">
<form class="form-horizontal" role="form" action="/user-management" method="post">
<div class="form-group">
<label for="user-name" class="col-sm-2 control-label">Uživatel</label>
<div class="col-sm-10">
<input id="user-name" class="autocomplete form-control" type="text"
placeholder="Jméno uživatele" name="username"/>
</div>
<input type="hidden"
th:name="${_csrf.parameterName}"
th:value="${_csrf.token}"/>
</div>
<div id="user-roles-form" th:fragment="roles(roles)" xmlns:th="http://www.thymeleaf.org">
<div class="form-group">
<label for="user-roles" class="col-sm-2 control-label">Uživatelovy role</label>
<div class="col-sm-10">
<select multiple="multiple" class="form-control" id="user-roles" name="rolesss">
<th:block th:each="role : ${roles}">
<option th:value="${role.userRole.role}" th:text="${role.userRole.role}" th:selected="${role.selected}"></option>
</th:block>
</select>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-ar btn-primary">Uložit</button>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
</section>
</th:block>
try using like below in your controller 尝试在控制器中像下面一样使用
in your HTML or JSP 在您的HTML或JSP中
<form modelAttribute="your_model_name"></form>
if you are using model attribute then use @ModelAttribute
如果您使用模型属性,则使用@ModelAttribute
otherwise, use @RequestParam("username")
否则,请使用@RequestParam("username")
In your case 就你而言
@RequestMapping(value = "", method = RequestMethod.POST)
public ModelAndView changeRoles(@RequestParam("username") String username,@RequestParam("rolesss") List<String> rolesss) {
..........
.........
return null;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.