乐趣区

SpringMVC-跨重定向请求传递数据

SpringMVC 跨重定向请求传递数据

我们知道, 重定向其实是两个请求, 而在第一次请求中的数据在第二次请求中出现的
那么如果我们需要重定向的功能而且希望第一次请求的数据能复制到下一次的请求中, 该如何实现呢?

  1. 通过在 url 中拼接参数

       return "redirect:/index?name=zhangsan&age=18"
      
         ````java
            // 完整代码
               public class HelloController {@GetMapping("hello")
                                public String hello(){return "redirect:index?name=zhangsan&&age=18";}
                                                     
                                                            @GetMapping("index")
                                                                   public String index(@RequestParam("name") String name, String age, Model model){System.out.println(name + "-" + "age");
                                                                                         return "index";
                                                                                                }
                                                                                                   }
                                                                                                      ````
                                                                                                      
                                                                                                         这样, 在重定向的下一次请求中我们就可以获取到 name 和 age 了
                                                                                                         
                                                                                                         2. 通过 RedirectAttributes
    

RedirectAttributes 是 Spring mvc 3.1 版本之后出来的一个功能,专门用于重定向之后还能带参数跳转的, 他有两种带参的方式:

  1. 第一种:**attr.addAttribute("name", "zhangshan");**  

  这种方式就 ** 相当于重定向 ** 之后,在 url 后面拼接参数,这样在重定向之后的页面或者控制器再去获取 url 后面的参数就可以了,但这个方式因为是在 url 后面添加参数的方式,所以 ** 暴露了参数,有风险 **
  
        ![](images/TIM 截图 20190712192040.png)
        
              ````java
                        @GetMapping("hello")
                                  public String hello(RedirectAttributes redirectAttributes){redirectAttributes.addAttribute("name", "zhangshang");
                                                              redirectAttributes.addAttribute("age", 18);
                                                                            return "redirect:index";
                                                                                      }
                                                                                            ````
                                                                                            
                                                                                               2. 第二种: **attr.addFlashAttribute("name", "zhangsna")**

  使用这种方式不会在 URL 后面追加参数
  
        ```java
                  @GetMapping("hello")
                            public String hello(RedirectAttributes redirectAttributes){redirectAttributes.addFlashAttribute("name", "zhangsan");
                                                        redirectAttributes.addFlashAttribute("age", "18");
                                                                      return "redirect:index";
                                                                                }
                                                                                          // 使用 @ModelAttribute 来获取参数
                                                                                                  @GetMapping("index")
                                                                                                            public String index(@ModelAttribute String name, @ModelAttribute String age){System.out.println(name + "-" + age);
                                                                                                                                        return "index";
                                                                                                                                                  }
                                                                                                                                                        ```
                                                                                                                                                        
                                                                                                                                                        最后我们都可以使用 th:text 标签在页面中取得这个值
                                                                                                                                                        
                                                                                                                                                        ````html
                                                                                                                                                        <h3 th:text="${name}"></h3>
                                                                                                                                                        <h3 th:text="${age}"></h3>
                                                                                                                                                        ````
退出移动版