【SpringMVC】重定向和转向详解

219 阅读3分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

1、本文内容3个知识点

  • SpringMVC中转发如何实现?
  • SpringMVC重定向如何实现?
  • 重定向3种传参方式

2、转发

2.1、servlet原生实现转发

request.getRequestDispatcher(path).forward(request,response);

2.2、SpringMVC实现转发

接口需满足下面这3条的会被SpringMVC当做转发进行处理

  • 接口返回值为String类型
  • 返回值格式:forward:转发的路径
  • 方法或者类上不要标注@ResponseBody注解

案例代码如下,当访问/forward/test1的时候,返回值以forward:开头,SpringMVC会将请求转发到/forward/test2

@RequestMapping("/forward/test1")
public String test1() {   
    return "forward:/forward/test2";
}

@RequestMapping(value = "/forward/test2", produces = "text/html;charset=UTF-8")
@ResponseBodypublic String test2() { 
    return "我是test2";
}

测试效果:浏览器中访问/forward/test1输出

63a3939c-e5f9-4d87-95aa-e5d80f81a67a.png

3、重定向

3.1、servlet原生实现重定向

response.sendRedirect(url);

3.2、SpringMVC实现重定向

接口需满足下面这3条的会被SpringMVC当做转发进行处理

  • 接口返回值为String类型
  • 返回值格式:redirect:重定向的路径
  • 方法或者类上不要标注@ResponseBody注解

案例代码如下,当访问/redirect/test1的时候,返回值以redirect:开头,SpringMVC会将请求转发到/redirect/test2

@RequestMapping("/redirect/test1")
public String test1() {   
    return "redirect:/redirect/test2";
}

@RequestMapping(value = "/redirect/test2", produces = "text/html;charset=UTF-8")
@ResponseBodypublic String test2() { 
    return "我是test2";
}

测试效果:浏览器中访问/redirect/test1,效果如下,浏览器地址栏变成/redirect/test2

9c662d2e-07af-429c-9e26-c6eea3c6ad87.png

3.3、重定向传参方式1:手动在地址后拼接参数

直接在重定向的url中直接拼接参数,如redirect:地址?name=腾腾&age=25,如

@RequestMapping("/redirect/test1")
public String test1() {  
    return "redirect:/redirect/test2?name=腾腾&age=25";
}

3.4、重定向传参方式2:RedirectAttributes.addAttribute(“参数”,”值”)

用法

  • 接口中需要有一个类型为RedirectAttributes的参数
  • 调用redirectAttributes.addAttribute("参数","值"),放入重定向需要传递的参数
  • 原理: 通过redirectAttributes.addAttribute丢进去的参数,SpringMVC重定向的时候,会自动将这些参数以?参数1=值1&参数2=值2拼接到重定向的地址上,类似于上面的方式1。

案例代码

访问接口test3,会被重定向到test4,顺便传递了2个参数

@RequestMapping("/redirect/test3")
public String test3(RedirectAttributes redirectAttributes) {    
    redirectAttributes.addAttribute("name", "腾腾");   
    redirectAttributes.addAttribute("age", 25);  
    return "redirect:/redirect/test4";
}

@RequestMapping(value = "/redirect/test4", produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public Map<String, Object> test4(@RequestParam("name") String name, @RequestParam("age") int age) { 
    Map<String, Object> result = new LinkedHashMap<>();   
    result.put("name", name);  
    result.put("age", age);  
    return result;
}

测试效果

浏览器中访问/redirect/test3接口,会被重定向到/redirect/test4,效果如下,test3方法中丢到addAttribute中的2个参数nameage,被自动拼接到地址后面了。

2451eccc-e435-4a1a-b8af-d54a62e4a66e.png

3.5、重定向传参方式2:RedirectAttributes.addFlashAttribute(“参数”,”值”)

上面我们使用的是RedirectAttributesaddAttribute放入参数,这次我们要使用另外一个方法addFlashAttribute放入重定向需要传递的参数,具体有什么区别呢,请向下看。

用法

  • 接口中需要有一个类型为RedirectAttributes的参数
  • 调用redirectAttributes.addFlashAttribute("参数","值"),这种方式传递的参数是被隐藏的,不会被拼接在地址后,内部是通过session共享数据来实现的。
  • 被重定向到的接口,需要使用一个org.springframework.ui.Model或者org.springframework.ui.ModelMap类型的参数来接收传递过来的参数,调用model.getAttribute("参数名")可以获取传递过来的参数

案例代码

访问接口test5,会被重定向到test6,顺便传递了2个参数

@RequestMapping("/redirect/test5")
public String test5(RedirectAttributes redirectAttributes) {  
    redirectAttributes.addFlashAttribute("name", "腾腾");  
    redirectAttributes.addFlashAttribute("age", 25); 
    return "redirect:/redirect/test6";
}

/** 
* 需要使用一个org.springframework.ui.Model或者org.springframework.ui.ModelMap类型的参数来接收传递过来的参数,
* 方法内部调用model.getAttribute("参数名")可以获取传递过来的参数 * * @param model 
* @return
*/
@RequestMapping(value = "/redirect/test6", produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public Map<String, Object> test6(Model model) {  
    String name = (String) model.getAttribute("name"); 
    Integer age = (Integer) model.getAttribute("age");  
    Map<String, Object> result = new LinkedHashMap<>();  
    result.put("name", name);   
    result.put("age", age);  
    return result;
}

测试效果

浏览器中访问/redirect/test5接口,会被重定向到/redirect/test6,效果如下,参数传递成功了,传递是隐藏式的。

b7687158-a77f-4422-b6e8-eca7bf71a611.png

原理

redirectAttributes.addFlashAttribute 放入重定向需要传递的参数,SpringMVC在重定向到新地址之前,会将这部分数据丢到session中,当重定向的请求过来后,SpringMVC又会从session中拿到这部分数据,然后丢到Model或者ModelMap中,然后冲session中清理掉这部分数据。

3.6、RedirectAttributes.addAttribute和RedirectAttributes.addFlashAttribute区别

  • 都可以实现重定向传递参数
  • addAttribute传递的参数,最后会追加在新的地址上,而addFlashAttribute传递的参数是隐藏式的
  • addFlashAttribute可以传递大量的信息,不过addFlashAttribute有个弊端,重定向到新地址之后,如下图,如果此时用户刷新页面,传递的参数取不到了,就丢失了,建议使用方式1和方式2;方式3可以作为了解。

总结:是不是很简单