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 来获取;
相关文章
相关标签/搜索