目录

我的学习分享

记录精彩的程序人生

X

【Spring】RedirectAttributes的使用

1. 解义:

    Spring提供了通过RedirectAttributes设置flash属性的方法,目的是将数据存入会话中使其能够长期保存并能跨多个请求,在重定向之后,从会话中将其取出。这是Spring3.1引入Model的一个子接口,提供了model的所有功能。工作运行图如下:

10027569312ef26149454ecf.png
flash属性保存在会话中,然后在放入模型中,因此能够中在重定向的过程中存活

2.使用:

 RedirectAttributes提供了一组addFlashAttribute()方法来添加flash属性,使用案例如下:

2.1 存值:

        @RequestMapping(value ="/customer/update",  method = RequestMethod.GET)

      public String updateCustomer(CustomerVo customerVo,RedirectAttributesmodel) {

            model.addFlashAttribute("customer");//or model.addFlashAttribute("customer",customerVo);

             return "redirect:/customer/get";

        }

2.2 取值:

        @RequestMapping(value ="/customer/get",  method = RequestMethod.GET)

         public String getCustomer(Model model) {

                if( !model.containsAttribute("customer") ){

                    //    从库里找信息返回到页面

                }

                return "/show";    

        }

作者:静翔_225d
链接:https://www.jianshu.com/p/ad2e7cb415b6
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。