-
Redirecting with URL templates
@RequestMapping(value = "save") public String saveDutyRelation(HttpServletRequest request, DutyRelation dutyRelation, Model model) throws Exception { try { requireNonNull(dutyRelation.getId(), "ID不能为空"); requireNonNull(dutyRelation.getDutyCode(), "职务编码不能为空"); dutyRelationService.updateById(dutyRelation, dutyRelation.getId()); result.setMessageSuccess("保存成功"); } catch (BusinessException e) { result.setCode(e.getCode()); result.setMessage(e.getMessage()); } model.addAttribute("username", dutyRelation.getId()); model.addAttribute("id", dutyRelation.getId()); model.addAttribute("code", dutyRelation.getDutyCode()); return "redirect:/dutyRelation/{username}"; }
-
Working with flash attrbutes
@RequestMapping(value = "save") public String saveDutyRelation(HttpServletRequest request, DutyRelation dutyRelation, RedirectAttributes attributes) throws Exception { result = new Result(); try { requireNonNull(dutyRelation.getId(), "ID不能为空"); requireNonNull(dutyRelation.getDutyCode(), "职务编码不能为空"); dutyRelationService.updateById(dutyRelation, dutyRelation.getId()); result.setMessageSuccess("保存成功"); } catch (BusinessException e) { result.setCode(e.getCode()); result.setMessage(e.getMessage()); } attributes.addAttribute("username", dutyRelation.getId()); attributes.addFlashAttribute(dutyRelation); // 可以根据类型推断 key 是 “dutyRelation” return "redirect:/dutyRelation/{username}"; }
原理:Flash attrbutes are stored in the session and then retrieved into the modal, surviving a redirect