错误:如标题
问题描述:
<div class="control-group">
<label for="resOpsList" class="control-label">对资源的操作:</label>
<div class="controls">
<c:forEach items="${resOpMap}" var="map">
<c:out value="${map.key}" />
<br>
<c:forEach items="${map.value}" var="resOp">
<input type="checkbox" name="resOps" value="${resOp.id}" <c:if test="${resOp.isSelect==1}">checked="true"</c:if>/>
<c:out value="${resOp.op.name}" />
</c:forEach>
<br>
<br>
</c:forEach>
</div>
</div>
点击提交时无法跳转到下面的Controller方法:
/**
* 给角色分配资源操作
*
* @return
*/
@RequestMapping(value = "addResAndOp", method = RequestMethod.POST)
public String addResAndOp(@Valid @ModelAttribute("role") Role role,/**
* 给角色分配资源操作
*
* @return
*/
@RequestMapping(value = "addResAndOp", method = RequestMethod.POST)
public String addResAndOp(@Valid @ModelAttribute("role") Role role,@RequestParam(value = "resOps") List<Long> selectIds,RedirectAttributes redirectAttributes) {
role.getResOpList().clear();
for (Long id : selectIds) {
role.getResOpList().add(new ResourceOp(id));
}
roleService.addRole(role);
return "redirect:/admin/role";
} List<Long> selectIds,RedirectAttributes redirectAttributes) {
role.getResOpList().clear();
for (Long id : selectIds) {
role.getResOpList().add(new ResourceOp(id));
}
roleService.addRole(role);
return "redirect:/admin/role";
}
原因:jsp中checkBox属性name的值原本为resOPList,下面方法的
@RequestParam(value = "resOps")的resOp原本为resOpList,
错误信息是:Field error in object 'role' on field 'resOpList': rejected value [8,9,10]; codes [typeMismatch.role.resOpList,typeMismatch.resOpList,typeMismatch.java.util.List,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [role.resOpList,resOpList]; arguments []; default message [resOpList]]; defaul........
解决方法是把resOpList改为一个另外的名称。