SpringMVC之转发和重定向

242 阅读2分钟

本文学习一下SpringMVC 中转发如何实现?SpringMVC 重定向如何实现?重定向 3 种传参方式

转发

servlet 原生实现转发

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

SpringMVC 实现转发

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

  • 接口返回值为 String 类型

  • 返回值格式:forward:转发的路径

  • 方法或者类上不要标注@ResponseBody 注解

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

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

重定向

servlet 原生实现重定向

response.sendRedirect(url);

SpringMVC 实现重定向

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

  • 接口返回值为 String 类型

  • 返回值格式:redirect:重定向的路径

  • 方法或者类上不要标注@ResponseBody 注解

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

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

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

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

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

重定向传参方式 2:RedirectAttributes.addAttribute("参数","值")

用法

  • 接口中需要有一个类型为RedirectAttributes的参数

  • 调用redirectAttributes.addAttribute("参数","值"),放入重定向需要传递的参数

  • **原理:**通过redirectAttributes.addAttribute丢进去的参数,SpringMVC 重定向的时候,会自动将这些参数以?参数1=值1&参数2=值2拼接到重定向的地址上,类似于上面的方式 1。

@RequestMapping("/redirect/test3")
public String test3(RedirectAttributes redirectAttributes) {
    redirectAttributes.addAttribute("name", "路人");
    redirectAttributes.addAttribute("age", 30);
    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;
}

重定向传参方式 3:RedirectAttributes.addFlashAttribute("参数","值")

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

用法

  • 接口中需要有一个类型为RedirectAttributes的参数

  • 调用redirectAttributes.addFlashAttribute("参数","值"),这种方式传递的参数是被隐藏的,不会被拼接在地址后,内部是通过 session 共享数据来实现的。

  • 被重定向到的接口,需要使用一个org.springframework.ui.Model或者org.springframework.ui.ModelMap类型的参数来接收传递过来的参数,调用model.getAttribute("参数名")可以获取传递过来的参数

@RequestMapping("/redirect/test5")
public String test5(RedirectAttributes redirectAttributes) {
    redirectAttributes.addFlashAttribute("name", "路人");
    redirectAttributes.addFlashAttribute("age", 30);
    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;
}

RedirectAttributes.addAttribute 和 RedirectAttributes.addFlashAttribute 区别

  • 都可以实现重定向传递参数

  • addAttribute 传递的参数,最后会追加在新的地址上,而 addFlashAttribute 传递的参数是隐藏式的

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