Spring RedirectAttributes參數跳轉代碼實例
RedirectAttributes 是Spring mvc 3.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')String prama)注解,采用傳參的方式。頁面獲值例如:
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')public List<Student> list(@RequestPrama(value = 'prama1')String prama1, @RequestPrama(value = 'prama2')String prama2,...){ //TODO //your code}
通過在controller中的list方法體中可以獲取到參數值。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。
相關文章:
