SpringMVC中使用forward和redirect进行转发和重定向以及重定向时如何传参详解
转自:http://blog.51cto.com/983836259/1877188
如题所示,在SpringMVC中可以使用forward和redirect关键字在Controller中对原请求进行转发或重定向到其他的Controller。比如可以这样使用:
package cn.zifangsky.controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView; import cn.zifangsky.model.User; @Controller
public class TestController { @RequestMapping("/user/index.html")
public ModelAndView userIndex() {
return new ModelAndView("user/index");
} @RequestMapping("/testForward.html")
public ModelAndView testForward(@RequestParam("username") String username){
ModelAndView mAndView = new ModelAndView("forward:/user/index.html"); User user = new User();
user.setName(username);
mAndView.addObject("user", user); return mAndView;
} @RequestMapping("/testRedirect.html")
public ModelAndView testRedirect(@RequestParam("username") String username){
ModelAndView mAndView = new ModelAndView("redirect:/user/index.html"); User user = new User();
user.setName(username);
mAndView.addObject("user", user); return mAndView;
}
}
然后项目启动后,在浏览器中访问:http://localhost:9180/CookieDemo/testForward.html?username=forward
页面显示效果如下:

可以看出,在使用forward进行转发时请求的URL链接是不会改变的
接着,在浏览器中访问:http://localhost:9180/CookieDemo/testRedirect.html?username=redirect
页面显示效果如下:

可以看出,在使用redirect进行重定向时请求的URL链接发生了改变,并且在controller中放置的参数并没有传递进行。那么,如果想要在重定向时把参数也传递过去应该怎么做呢?
方法一:常规做法,重定向之前把参数放进Session中,在重定向之后的controller中把参数从Session中取出并放进ModelAndView
示例代码如下:
@RequestMapping("/user/index.html")
public ModelAndView userIndex(HttpServletRequest request) {
ModelAndView mAndView = new ModelAndView("user/index");
HttpSession session = request.getSession();
mAndView.addObject("user", session.getAttribute("user"));
session.removeAttribute("user");
return mAndView;
}
@RequestMapping("/testRedirect.html")
public ModelAndView testRedirect(@RequestParam("username") String username,HttpServletRequest request){
ModelAndView mAndView = new ModelAndView("redirect:/user/index.html");
User user = new User();
user.setName(username);
request.getSession().setAttribute("user", user);
return mAndView;
}
方法二:使用RedirectAttributes类
示例代码如下:
@RequestMapping("/user/index.html")
public ModelAndView userIndex() {
return new ModelAndView("user/index");
}
@RequestMapping("/testRedirect.html")
public ModelAndView testRedirect(@RequestParam("username") String username,RedirectAttributes redirectAttributes){
ModelAndView mAndView = new ModelAndView("redirect:/user/index.html");
User user = new User();
user.setName(username);
// redirectAttributes.addAttribute("user", user); //URL后面拼接参数
redirectAttributes.addFlashAttribute("user", user);
return mAndView;
}
使用RedirectAttributes这个类来传递参数写法就很简单了,只需要在需要重定向的controller的方法参数中添加RedirectAttributes类型的参数,然后把需要重定向之后也能够获取的参数放进去即可
当然,据说RedirectAttributes本质上也是通过Session来实现的(PS:相关源代码我没看过,只能据说了)。实际上上面的代码是省略的写法,因为重定向之后就直接返回页面视图了,如果经过几次重定向的话估计上面那种写法就获取不到参数了,因此关于获取参数一般还有以下两种方式:
(1)使用@ModelAttribute注解获取参数:
@RequestMapping("/user/index.html")
public ModelAndView userIndex(@ModelAttribute("user") User user) {
ModelAndView mAndView = new ModelAndView("user/index");
mAndView.addObject("user", user);
return mAndView;
}
(2)使用RequestContextUtils类来获取:
@RequestMapping("/user/index.html")
public ModelAndView userIndex(HttpServletRequest request) {
ModelAndView mAndView = new ModelAndView("user/index");
Map<String, Object> map = (Map<String, Object>) RequestContextUtils.getInputFlashMap(request);
User user = (User) map.get("user");
mAndView.addObject("user", user);
return mAndView;
}
总结,这两种获取方式都可以成功获取到参数。但是还是略显麻烦,如果只是一次重定向之后就返回页面视图的话推荐使用最简单那种写法
SpringMVC中使用forward和redirect进行转发和重定向以及重定向时如何传参详解的更多相关文章
- Java中对象、对象引用、堆、栈、值传递以及引用传递的详解
Java中对象.对象引用.堆.栈.值传递以及引用传递的详解 1.对象和对象引用的差别: (1).对象: 万物皆对象.对象是类的实例. 在Java中new是用来在堆上创建对象用的. 一个对象能够被多个引 ...
- SpringMVC视图解析中的 forward: 与 redirect: 前缀
在 SpringMVC 中,可以指定画面的跳转方式.使用 forward: 前缀实现请求转发跳转,使用 redirect: 前缀实现重定向跳转.有前缀的转发和重定向操作和配置的视图解析器没有关系,视图 ...
- SpringMvc(4-1)Spring MVC 中的 forward 和 redirect
Spring MVC 中,我们在返回逻辑视图时,框架会通过 viewResolver 来解析得到具体的 View,然后向浏览器渲染.通过配置,我们配置某个 ViewResolver 如下: <b ...
- SpringMvc(4-1)Spring MVC 中的 forward 和 redirect(转)
Spring MVC 中,我们在返回逻辑视图时,框架会通过 viewResolver 来解析得到具体的 View,然后向浏览器渲染.通过配置,我们配置某个 ViewResolver 如下: <b ...
- Spring MVC 中的 forward 和 redirect
Spring MVC 中,我们在返回逻辑视图时,框架会通过 viewResolver 来解析得到具体的 View,然后向浏览器渲染.假设逻辑视图名为 hello,通过配置,我们配置某个 ViewRes ...
- jsp中jsp:forward 与 redirect区别
部分转载:http://hi.baidu.com/168zlf/item/2f4b2ad4351b881c20e2500c 在网上看到一些帖子,总结了一些区别,可以从以下几个方面来看: 1.从地址栏显 ...
- c++中的构造(包括移动),赋值(包括移动),析构详解
这五种操作:构造(包括移动),赋值(包括移动),析构其实就是定义了对一个对象进行构造,赋值,析构时的行为.理解这些行为并不复杂,复杂的是理解在继承下这些行为的表现.需要注意的是他们并不会被继承(传统意 ...
- servlet中的forward()和redirect()
从地址栏显示来说 forward是服务器请求资源,服务器直接访问目标地址的URL,把那个URL的响应内容读取过来,然后把这些内容再发给浏览器 浏览器根本不知道服务器发送的内容从哪里来的,所以它的地址栏 ...
- jsp中的forward和redirect的区别
转自http://blog.163.com/tsing_hua/blog/static/139622224201101110836644/ 一.调用方式 我们知道,在servlet中调用转发.重定向的 ...
随机推荐
- Linux嵌入式 -- 内核 - 内核定时器
1. 度量时间差 时钟中断由系统的定时硬件以周期性的时间间隔产生,这个间隔(即频率)由内核根据HZ来确定,HZ是一个与体系结构无关的常数,可配置(50-1200),在X86平台,默认值为1000(每 ...
- AOP理解,待细看
http://jinnianshilongnian.iteye.com/blog/1474325
- js职责链模式
职责链模式(Chain of Responsiblity),使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系.将这个对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为 ...
- 最实用的 Linux 命令行使用技巧
我们可能每天都会要使用到很多的 Linux 命令行. 我们也会网络上知晓一些使用它们的小技巧,但是如果我们没有时常来进行练习,就有可能会忘掉怎么去使用它们. 所以我就决定把那些你可能会忘记的小提示和小 ...
- 《Advanced Bash-scripting Guide》学习(六):从/etc/fstab中读行
本文所选的例子来自于<Advanced Bash-scripting Gudie>一书,译者 杨春敏 黄毅 ABS书上的例子: 代码块和I/O重定向 #!/bin/bash #从/etc/ ...
- tableau学习笔记—1
第一部分 第一章 数据可视化 1.1 用数据讲故事 1.2 数据不只是数字 1.3 在数据中寻找什么(关系.模式.异常) 第二章 Tableau概述 2.1 Tableau概述 2.2 产品简介 第三 ...
- Phone numbers
Phone number in Berland is a sequence of n digits. Often, to make it easier to memorize the number, ...
- [HDU5324]Boring Class
vjudge sol 字典序最小可以通过倒着\(dp\)解决.对每个\(i\)记录它可以转移到的\(dp\)值最大且字典序最小的\(nxt_i\). 尝试着写一下\(dp\)式子. \[dp_i=ma ...
- NOIp2018集训test-10-24(am&pm)
李巨连续AK三场了,我跟南瓜打赌李巨连续AK七场,南瓜赌李巨连续AK五场. DAY1 T1 qu 按题意拿stack,queue和priority_que模拟即可.特判没有元素却要取出的情况. T2 ...
- 使用sort&awk实现文件内容块排序
源文件为: [root@luo5 wangxx]# cat -v luo.txt J LuoSoutth jfsaNanjing,china Y ZhangVictory UniversityNejf ...