Spring RedirectAttributes: addAttribute vs addFlashAttribute
来源:互联网 发布:美工设计岗位要求 编辑:程序博客网 时间:2024/05/18 01:31
My understanding so far is on our controller request mapping method we can specify RedirectAttributes
parameter and populate it with attributes for when the request get redirected, eg:
@RequestMapping(value="/hello", method=GET)public String hello(RedirectAttributes redirAttr){ // should I use redirAttr.addAttribute() or redirAttr.addFlashAttribute() here ? // ... return "redirect:/somewhere";}
The redirect attributes will then be available on the target page where it redirects to.
However RedirectAttributes
have two methods: addAttribute
& addFlashAttribute
. What is the fundamental differences between those two, and how should I choose which one to use? Have been reading spring manual doc for a while but I’m a bit lost
·addFlashAttribute· actually stores the attributes in a flashmap (which is internally maintained in the users session and removed once the next redirected request gets fulfilled), on the other addAttribute
essentially constructs request parameters out of your attributes and redirects to the desired page with the request parameters. So the advantage will be that you can store pretty much any object in your flash attribute(as it is not serialized into request params at all, but maintained as an object), whereas with addAttribute
since the object that you add gets transformed to a normal request param, you are pretty limited to the object types like String
or primitives.
Javadoc description: “A FlashMap provides a way for one request to store attributes intended for use in another. This is most commonly needed when redirecting from one URL to another – e.g. the Post/Redirect/Get pattern. A FlashMap is saved before the redirect (typically in the session) and is made available after the redirect and removed immediately.”
Assume you have 2 controllers.If you redirect from one controller to another controller the values in model object won’t be available in the other controller. So if you want to share the model object values then you have to say in first controller
addFlashAttribute("modelkey", "modelvalue");
Then second controller’s model contains now the above key value pair..
Second question ? What is difference between addAttribute and addFlashAttribute in RedirectAttributes class
addAttribute will pass the values as requestparameters instead of model,so when you add some using addAttribute you can access those values from request.getParameter
Here is the code.I have used to find out what is going on :
@RequestMapping(value = "/rm1", method = RequestMethod.POST)public String rm1(Model model,RedirectAttributes rm) {System.out.println("Entered rm1 method ");rm.addFlashAttribute("modelkey", "modelvalue");rm.addAttribute("nonflash", "nonflashvalue");model.addAttribute("modelkey", "modelvalue");return "redirect:/rm2.htm";}@RequestMapping(value = "/rm2", method = RequestMethod.GET)public String rm2(Model model,HttpServletRequest request) {System.out.println("Entered rm2 method ");Map md = model.asMap();for (Object modelKey : md.keySet()) {Object modelValue = md.get(modelKey);System.out.println(modelKey + " -- " + modelValue);}System.out.println("=== Request data ===");java.util.Enumeration<String> reqEnum = request.getParameterNames();while (reqEnum.hasMoreElements()) {String s = reqEnum.nextElement();System.out.println(s);System.out.println("==" + request.getParameter(s));}return "controller2output";}
- Spring RedirectAttributes: addAttribute vs addFlashAttribute
- addAttribute和addFlashAttribute的区别
- RedirectAttributes对象addFlashAttribute()的使用
- RedirectAttributes对象addFlashAttribute()的使用
- Spring MVC addFlashAttribute
- spring mvc3中的addFlashAttribute方法
- spring mvc3中的addFlashAttribute方法
- spring mvc3中的addFlashAttribute方法
- spring mvc3中的addFlashAttribute方法
- spring mvc3中的addFlashAttribute方法
- Spring 中的RedirectAttributes 功能
- Spring中RedirectAttributes说明
- spring mvc RedirectAttributes 的使用
- Spring中RedirectAttributes的用法
- Spring MVC RedirectAttributes的用法
- RedirectAttributes
- RedirectAttributes
- Spring中RedirectAttributes类的具体用法
- 两款Flash上传插件(1)--CFUpdate文件批量上传组件
- 学习小结20150921--声明和头部元素
- MBProgressHUD第三方库源码分析
- 【网络技术】网络技术简析
- 【设计模式】——依赖倒转原则
- Spring RedirectAttributes: addAttribute vs addFlashAttribute
- sudo: /etc/sudoers is mode 0777, should be 0440终极解决之道
- Java____小数位、百分号相关
- eclipse中读取nutch爬取的数据内容
- 摄氏温度转化为相应的华氏温度
- 就业窍门:1.想好自己打算将来干什么
- xFire调用wsdl webservice超时时间设置
- MYSQL启动报1067错误 MySQL服务无法启动 或者Can't create/write to file#sql6e80_1905f7_3.MYI'
- [blabla]a quick code about linear regression using gradient descent