spring 頁面跳轉不到對應的Controller方法
阿新 • • 發佈:2019-01-26
錯誤:如標題
問題描述:
<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改為一個另外的名稱。