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中调用转发.重定向的 ...
随机推荐
- numpy random 生成随机矩阵
import numpy as np np.random.rand(a, b): >>> np.random.rand(4,3) array([[ 0.06679473, 0.710 ...
- C/C++输入数组
; printf("please enter the number:\n"); scanf("%d",&n); int *number=new int[ ...
- 牛客练习赛7E 珂朵莉的数列
题意:求所有子区间的逆序数对数之和 题解:树状数组维护,对于每一对逆序数(l,r)属于l*(n-r+1)个区间,计算每一对对结果的贡献即可,可用树状数组维护,sum维护(n-r+1),按逆序数那样操作 ...
- hdu 2147 kiki's game(找规律)
kiki's game Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 40000/10000 K (Java/Others)Total ...
- LeetCode OJ:Validate Binary Search Tree(合法的二叉搜索树)
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- java学习笔记--常用类
一.Math类:针对数学运算进行操作的类 1.常用的方法 A:绝对值 public static int abs(int a) B:向上取整 public static double ceil( ...
- Alex and broken contest CodeForces - 877A
/* Name: Copyright: Author: Date: 2018/5/2 10:45:16 Description: 要求出现一个朋友的名字,仅一次 */ #include <ios ...
- ios开发小技巧(转)
1.通过下面方式可以获取图片的像素颜色点:- (void*)getImageData:(UIImage*)image{ void* imageData; if (imageData == ...
- Project://CRM
初始化 考勤 录入成绩 查看成绩 待续...
- Python ord()与chr()函数
chr():十进制或十六进制数(0-255)转成对应的ASCII字符. ord():ASCII字符转成对应的十进制数. 一个小性质:ASCII表中大写字母排在前面小写排在后面,相差32. 比如: or ...