探寻SpringMVC重写和转发版权声明
原创在最近参与的一个微信公号相关项目的开发中,业务包括大量的页面跳转逻辑,以及拦截器的数据采集验证。同时,我在探索中也遇到了一些困惑。 Spring MVC 中 redirect 和 forward 在编写完源代码后,对经验进行了总结和整理,从而做出本文。
例如,客户的请求。 Controller 方法时,我们会判断当前的用户状态,可以跳转到用户中心页面,也可以跳转到等待页面,或者出错页面。有许多类似的场景需要重定向和转发请求。Sping MVC 有许多方法可以重定向或转发。我会先整理一下,然后通过源代码加深我的理解。
常见的治疗方法
Controller 视图方法之间的跳转无非是带参数的跳转和不带参数的跳转。常用的方法都是直通的。 String 映射 RequestMapping 实现了重定向,或者 ModelAndView
对象,或 RedirectView
对象,下面逐一描述。
String 重定向
是 return 映射到另一个 Controller 该方法的字符串。如果有请求参数,则将其拼接在一起。 RequestMapping 在映射的字符串之后。
1 2 3 4 5 6 7
// 返回字符串的映射方式。 @RequestMapping("hello") public String hello(HttpServletRequest req, HttpServletResponse resp) { doSomething(); return "redirect:/bye"; // return "redirect:/bye?username=sudoz"; }
ModelAndView 重定向
另一种方法是通过返回 ModelAndView
对象来实现跳转。同样,如果有请求参数,也可以传递SIMILAR。 GET 如何拼接参数:
1 2 3 4 5 6 7
// 返回 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 前缀:
1 2 3 4 5 6 7
// 返回 RedirectView 对象 @RequestMapping("hello") public RedirectView hello() { doSomething(); return new RedirectView("/bye"); // return new RedirectView("bye?username=sudoz"); }
带参跳转
在做方法跳转时,如果你想把参数带到下一个方法,就像上面的代码一样通过拼接 URL 参数法有时是不实用的。因为参数不一定是字符串,而浏览器是正确的。 URL 长度是有限制的。 RedirectAttributes
对象可用于在请求重定向时保存参数。利用率 RedirectAttributes
重写上面的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
@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 正确的。这两种方法都很常见,在使用上肯定有所不同:
addAttribute()
方法会把 Key-Value 作为请求参数添加 URL 的后面;addFlashAttribute()
方法会把 Key-Value 暂存在 session 在跳到目标方法之后,任务就完成了。 session 中删掉;
用 curl
用于测试的命令:
1 2 3 4 5 6 7 8
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 跳。另一种分发请求的方式是转发,即E。 forward。(我不确定这个翻译是否正确,所以我将直接在下面使用它。 redirect 和 forward 表示),两者之间的区别来自。 HTTP 说明书中明确规定:
- redirect 的 HTTP 返回码是 302跳到新的 URL 会存储在 HTTP Response Headers 的 Location 现场。客户端正在接收 Response 在此之后,将发起另一个请求,这次是请求。 URL 被重定向 URL;
- forward 转发过程仅发生在服务端;Servlet 容器会将源请求直接命中目标。 URL而不是通过客户端发起请求; URL 但不会改变,而且 forward 无法转发参数。
Spring Boot 测试
为了更清楚地比较各种方法,我把 Spring Boot 测试代码已发布。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
@Controller public class HomeController { @Value("sudoz") private String username;
@GetMapping("/")
String index() {
return "redirect:hello?username=jim&message=how are you";
}
@GetMapping("redirectView")
RedirectView redirectView() {
return new RedirectView("hello");
}
@GetMapping("forward")
ModelAndView forward(RedirectAttributes attrs) {
attrs.addFlashAttribute("username", username);
attrs.addAttribute("message", "hello");
return new ModelAndView("forward:hello");
}
@GetMapping("redirect")
ModelAndView redirect(RedirectAttributes attrs) {
attrs.addFlashAttribute("username", username);
attrs.addAttribute("message", "hello");
return new ModelAndView("redirect:hello");
}
@GetMapping("hello")
@ResponseBody
Map hello(Model model, @ModelAttribute("username") String username,
@ModelAttribute("message") String message) {
Map map = Maps.newHashMap();
map.put("username", username);
map.put("message", message);
return map;
}
}
分析源码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
public class UrlBasedViewResolver extends AbstractCachingViewResolver implements Ordered { public static final String REDIRECT_URL_PREFIX = "redirect:"; public static final String FORWARD_URL_PREFIX = "forward:";
@Override
protected View createView(String viewName, Locale locale) throws Exception {
// If this resolver is not supposed to handle the given view,
// return null to pass on to the next resolver in the chain.
if (!canHandle(viewName, locale)) {
return null;
}
// Check for special "redirect:" prefix.
if (viewName.startsWith(REDIRECT\_URL\_PREFIX)) {
String redirectUrl = viewName.substring(REDIRECT\_URL\_PREFIX.length());
RedirectView view = new RedirectView(redirectUrl, isRedirectContextRelative(), isRedirectHttp10Compatible());
return applyLifecycleMethods(viewName, view);
}
// Check for special "forward:" prefix.
if (viewName.startsWith(FORWARD\_URL\_PREFIX)) {
String forwardUrl = viewName.substring(FORWARD\_URL\_PREFIX.length());
return new InternalResourceView(forwardUrl);
}
// Else fall back to superclass implementation: calling loadView.
return super.createView(viewName, locale);
}
}
版权声明
所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除