Spring MVC 如何重定向

2017-10-09  本文已影响0人  小浩上校

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");
}

带参跳转

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

@RequestMapping("/")
public RedirectView hello(RedirectAttributes attrs) {
    attrs.addAttribute("message", "hello");    
    attrs.addFlashAttribute("username", "sudoz");
    return new RedirectView("hello");
}
@RequestMapping("hello")
    Map<String, String> hello(@ModelAttribute("username") String username,
                              @ModelAttribute("message") String message) {
    Map<String, String> map = Maps.newHashMap();
    map.put("username", username);
    map.put("message", message);
    return map;
}

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

redirect 和 forward 的区别

上面列出的 3 种方法,其实都是 Spring MVC 在处理请求时的重定向,即 redirect 跳转。另一种分发请求的方式是转发,即 forward。(我不确定这么翻译是否正确,所以下面就直接用 redirect 和 forward 来表述),二者的区别从 HTTP 的规范中就明确了:

上一篇 下一篇

猜你喜欢

热点阅读