java中RedirectAttributes類的使用

1、RedirectAttributes類簡介

  1. RedirectAttributes是Spring mvc 3.1版本以後出來的一個功能,專門用於重定向以後還能帶參數跳轉的工具類
  2. 使用此類引入包:import org.springframework.web.servlet.mvc.support.RedirectAttributes;
  3. 劃重點:用於重定向攜帶參數

2、類中經常使用方法介紹

  1. addAttributie方法web

    redirectAttributes.addAttributie("param1",value1);
           redirectAttributes.addAttributie("param2",value2);
           return "redirect:/path/list" ;

    注意:這個方法是用來跳轉的時候,將參數直接暴露在url中,等同於重定向到:return "redirect:/path/list?prama1=value1&param2=value2 "spring

  2. addFlashAttributie方法session

    redirectAttributes.addFlashAttributie("prama1",value1);
           redirectAttributes.addFlashAttributie("prama2",value2);
           return:"redirect:/path/list.jsp" ;

    注意:此方法是重定向的時候,param1和param2兩個參數在不暴露在url中隱藏的傳遞給list.jsp中
    原理:其原理就是放到session中,session在跳到頁面後立刻移除對象。mvc

  3. 以上兩種方法只能將參數重定向到頁面中,也就是視圖中用el表達式直接獲取你帶參的值,不能重定向到controller中獲取參數值
  4. 若是想要在controller中獲取傳遞到參數的值:jsp

    - 對於addAttributie方法傳遞的值使用@RequestParam("param1") String str 來獲取;
           - 對於addFlashAttribute方法傳遞的值使用@ModelAttribute("param1") String str 來獲取;
相關文章
相關標籤/搜索