区别
-
转发比重定向快 -
重定向经过客户端 - 若需要转到一个外部网站,只能使用
重定向 -
重定向可以避免用户重新加载页面时再次发起请求 -
转发只有一次请求,而重定向实际是两次请求
总结:
转发就好比你拿着请假条去找班长请假,班长知道这应该找班主任,所以就拿着你的申请去找班主任,然后签好字给你了,而重定向就比较懒了,他只告诉你,这该找班主任,然后你就得自己再自个去找班主任了
使用spring 3的Flash属性让重定向传值
我们知道由于重定向经过客户端,所以参数会在重定向的过程中丢失,但是在spring 3版本及以上,我们可以通过Flash属性进行传值,方法:
- spring mvc配置开启
<annotation-driven/> - 在Controller方法上添加
org.springframework.web.servlet.mvc.supportRedirectAttributes参数
示例:
@RequestMapping("/test")
public String test(RedirectAttributes redirectAttributes) {
redirectAttributes.addFlashAttribute("test", "msg");
return "redirect:/test2";
}