Carring data across redirect requests

简介: Redirecting with URL templates@RequestMapping(value = "save") public String saveDutyRelation(HttpServletRequest request, DutyRelation d...
  1. 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}";
    }

    重定向的地址是:http://**/dutyRealtion/username?id=某个值&code=某个值

  2. 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

目录
相关文章
|
前端开发 JavaScript 安全
AJAX - $().load(url,data,function(response,status,xhr))
AJAX - $().load(url,data,function(response,status,xhr))
59 0
Proxy error: Could not proxy request
Proxy error: Could not proxy request
2469 0
Proxy error: Could not proxy request
|
7月前
|
JSON 数据格式
这个错误信息表示在执行`requests.post(url, data=data, headers=head).json()`时出现了问题
这个错误信息表示在执行`requests.post(url, data=data, headers=head).json()`时出现了问题
102 2
|
XML 数据格式
svn报错-ra_serf: The server sent a truncated HTTP response body.
svn报错-ra_serf: The server sent a truncated HTTP response body.
524 1
|
JSON 数据格式
axios.post请求出错:Request header field content-type is not allowed by Access-Control-Allow-Headers in……
axios.post请求出错:Request header field content-type is not allowed by Access-Control-Allow-Headers in……
|
缓存 Linux Apache
Requests Header | Http Header
Requests Header | Http Header
|
KVM 虚拟化
成功解决Problem while trying to mount target]\“. HTTP response code is 400
成功解决Problem while trying to mount target]\“. HTTP response code is 400
requests--Cookie设置
requests--Cookie设置
|
前端开发 JavaScript
ajax请求的重定向处理--Request header field x-requested-with is not allowed by Access-Control-Allow-Header
ajax请求的重定向处理--Request header field x-requested-with is not allowed by Access-Control-Allow-Header
514 0