Spring RedirectAttributes参数跳转代码实例
RedirectAttributes是Springmvc3.1版本之后出来的一个功能,专门用于重定向之后还能带参数跳转的的工具类。他有两种带参的方式:
第一种:
redirectAttributes.addAttributie("prama",value);这种方法相当于在重定向链接地址追加传递的参数,例如:
redirectAttributes.addAttributie("prama1",value1);
redirectAttributes.addAttributie("prama2",value2);
return:"redirect:/path/list";
以上重定向的方法等同于return:"redirect:/path/list?prama1=value1&prama2=value2",注意这种方法直接将传递的参数暴露在链接地址上,非常的不安全,慎用。
第二种:
redirectAttributes.addFlashAttributie("prama",value);这种方法是隐藏了参数,链接地址上不直接暴露,但是能且只能在重定向的“页面”获取prama参数值。其原理就是放到session中,session在跳到页面后马上移除对象。如果是重定向一个controller中是获取不到该prama属性值的。除非在controller中用(@RequestPrama(value="prama")Stringprama)注解,采用传参的方式。页面获值例如:
redirectAttributes.addFlashAttributie("prama1",value1);
redirectAttributes.addFlashAttributie("prama2",value2);
return:"redirect:/path/list.jsp";
在以上参数均可在list.jsp页面使用EL表达式获取到参数值${prama*}
controller获得redirectAttributes重定向的值例如:
redirectAttributes.addFlashAttributie("prama1",value1); redirectAttributes.addFlashAttributie("prama2",value2); return:"redirect:/path/list/" @RequestMapping("list") publicListlist(@RequestPrama(value="prama1")Stringprama1, @RequestPrama(value="prama2")Stringprama2,... ){ //TODO //yourcode }
通过在controller中的list方法体中可以获取到参数值。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。