addAttributie和RedirectAttributes

1.redirectAttributes.addAttributie("prama",value);?

addAttribute()?essentially constructs?request parameters?out of your attributes and redirects to the desired page with the request parameters

这种方法相当于在重定向链接地址追加传递的参数

redirectAttributes.addAttributie("prama1",value1);?

redirectAttributes.addAttributie("prama2",value2);

?return:"redirect:/path/list"

以上重定向的方法等同于 return:"redirect:/path/list?prama1=value1&prama2=value2 " ,注意这种方法直接将传递的参数暴露在链接地址上,非常的不安全,慎用。

2.redirectAttributes.addFlashAttributie("prama",value);?

添加的属性放入flashmap中,session来维护这个flashmap

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)

So the?advantage?of?addFlashAttribute()?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.

这种方法是隐藏了参数,链接地址上不直接暴露

flashmap是放到session中,重定向后再从session中移除,在重定向的页面是用${prama}取数据的。如果是重定向一个controller中是获取不到该prama属性值的。

? ??????在重定向到的controller(action)怎么获取呢?

法1.利用Spring提供的标签@ModelAttribute("parma")

????public String test2(@ModelAttribute("parma") String str)

????{? ? ? System.out.println(str);? ? return "/test/hello";? ? ?}

法2.:利用httpServletRequest

public String test2(HttpServletRequest request)

{

Map map = RequestContextUtils.getInputFlashMap(request);

System.out.println(map.get("test").toString());

? ? return "/test/hello";

}