首页 > 编程技术 > java

详细聊聊Spring MVC重定向与转发

发布时间:2021-9-7 16:00

重定向和转发

重定向经过客户端,而转发没有,因此相对来说转发更快速。但有时采用重定向更方便,如:

  1. 重定向到外部网站;
  2. 避免用户重新加载页面时再次调用同样的动作。

return "redirect:/view/"+saveUser.getId();

这里使用重定向来防止当前用户重新加载页面时'''saveUser'''被二次调用。

但是使用重定向无法轻松地给目标页面传值,因此,在Spring3.1后提供了Flash属性,详情见后文。

常用处理方式

Controller 视图方法间的跳转,无非就是带参跳转和不带参跳转。常用的方法有通过 String 映射 RequestMapping 实现重定向,或者通过 ModelAndView 对象,又或者是 RedirectView 对象,下面逐一说明。

String 重定向

是 return 映射到另一个 Controller 方法的字符串。如果有请求参数,就拼接在 RequestMapping 映射的字符串后面。

// 返回字符串映射的方式
@RequestMapping("hello")
public String hello(HttpServletRequest req, HttpServletResponse resp) {
    doSomething();
    return "redirect:/bye";
    // return "redirect:/bye?username=sudoz";
}

ModelAndView 重定向

另一种方法是通过返回 ModelAndView 对象来实现跳转。类似的,如果有请求参数,也可以通过类似 GET 参数拼接的方式:

// 返回 ModelAndView 对象
@RequestMapping("hello")
public ModelAndView hello(HttpServletRequest req, HttpServletResponse resp) {
    doSomething();
    return new ModelAndView("redirect:/bye");
    // return new ModelAndView("redirect:/bye?username=sudoz");
}

RedirectView 重定向

还有一种方法是通过返回 RedirectView 对象实现跳转,该方法和上面的不同之处在于,RedirectView 对象不需要设置 redirect 前缀:

// 返回 RedirectView 对象
@RequestMapping("hello")
public RedirectView hello() {
    doSomething();
    return new RedirectView("/bye");
    // return new RedirectView("bye?username=sudoz");
}

带参跳转

Model在重定向时会丢失携带的消息

在做方法跳转时,如果要把参数带给下一个方法,像上面代码里通过拼接 URL 参数的方法有时候并不实用。因为参数不一定都是是字符串,而且浏览器对 URL 的长度是有限制的。RedirectAttributes 对象可以用来保存请求重定向时的参数。利用 RedirectAttributes 改写上面的代码:

@RequestMapping("/")
public RedirectView hello(RedirectAttributes attrs) {
    attrs.addAttribute("message", "hello");    
    attrs.addFlashAttribute("username", "world");
    return new RedirectView("hello");
}

@RequestMapping("hello")
    Map<String, String> hello(@ModelAttribute("message") String meaasge,
                              @ModelAttribute("username") String username) {
    Map<String, String> map = new HashMap();
    map.put("message", message);
    map.put("username", username);
    return map;
}

上面的代码中,调用 addAttribute() 和 addFlashAttribute() 方法往 RedirectAttributes 对象中插入了两个值,如果看源码,就能知道,RedirectAttributes 接口的实现类 RedirectAttributesModelMap 继承了 ModelMap,本质上就是 HashMap 的子类,因此可以用来存储 Key-Value 对。这两个方法都很常用,使用上也必然存在不同:

用 curl 命令来测试:

curl -i http://localhost:8080/

HTTP/1.1 302 
Set-Cookie: JSESSIONID=D1CC5E15FA8EF9474C4CED7D4F660E66;path=/;HttpOnly
Location: http://localhost:8080/hello;jsessionid=D1CC5E15FA8EF9474C4CED7D4F660E66?username=sudoz
Content-Language: en-US
Content-Length: 0
Date: Thu, 16 Feb 2017 12:33:46 GMT

可以看到,通过 addAttribute() 添加的键值对变成了 URL 后面的参数,addFlashAttribute() 方法添加的键值对则没有出现在 URL 上,而是存储在了 session 中。跳转的目标方法通过 @ModelAttribute("key")注解指定接收的参数。

redirect 和 forward 的区别

上面列出的 3 种方法,其实都是 Spring MVC 在处理请求时的重定向,即 redirect 跳转。另一种分发请求的方式是转发,即 forward。二者的区别从 HTTP 的规范中就明确:

附:请求转发与重定向的区别图例

总结

到此这篇关于Spring MVC重定向与转发的文章就介绍到这了,更多相关Spring MVC重定向与转发内容请搜索猪先飞以前的文章或继续浏览下面的相关文章希望大家以后多多支持猪先飞!

标签:[!--infotagslink--]

您可能感兴趣的文章: