SpringMVC中使用forward和redirect进行转发和重定向以及重定向时如何传参详解

简介:

如题所示,在SpringMVC中可以使用forward和redirect关键字在Controller中对原请求进行转发或重定向到其他的Controller。比如可以这样使用:

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
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

页面显示效果如下:

wKioL1g7i7bxN-UVAAAxxLSwXWU933.png

可以看出,在使用forward进行转发时请求的URL链接是不会改变的

接着,在浏览器中访问:http://localhost:9180/CookieDemo/testRedirect.html?username=redirect

页面显示效果如下:

wKiom1g7i9OAT7MjAAA36HQ7Kgw414.png

可以看出,在使用redirect进行重定向时请求的URL链接发生了改变,并且在controller中放置的参数并没有传递进行。那么,如果想要在重定向时把参数也传递过去应该怎么做呢?

方法一:常规做法,重定向之前把参数放进Session中,在重定向之后的controller中把参数从Session中取出并放进ModelAndView

示例代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@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类

示例代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
     @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注解获取参数:

1
2
3
4
5
6
7
@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类来获取:

1
2
3
4
5
6
7
8
9
10
@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;
}

总结,这两种获取方式都可以成功获取到参数。但是还是略显麻烦,如果只是一次重定向之后就返回页面视图的话推荐使用最简单那种写法



本文转自 pangfc 51CTO博客,原文链接:http://blog.51cto.com/983836259/1877188,如需转载请自行联系原作者

相关文章
|
17天前
|
Java 数据安全/隐私保护
response对象的转发和重定向
response对象的转发和重定向
6 0
|
3月前
|
存储 Java
转发,重定向(forward,sendRedirect)传输数据
转发,重定向(forward,sendRedirect)传输数据
|
3月前
springmvc请求转发和重定向的四种跳转方式
springmvc请求转发和重定向的四种跳转方式
|
4月前
|
存储 数据处理
Request请求转发与Respones请求重定向有什么区别?
Request请求转发与Respones请求重定向有什么区别?
|
4月前
|
Java Spring
SpringMVC中转发与重定向(redirect与forward)实践实例
SpringMVC中转发与重定向(redirect与forward)实践实例
51 0
|
5月前
forward内部跳转 和redirect重定向跳转的区别
forward内部跳转 和redirect重定向跳转的区别
|
10月前
|
Java
Java中forward转发 和 redirect重定向的区别
Java中forward转发 和 redirect重定向的区别
|
10月前
springmvc请求重定向
springmvc请求重定向
52 0
QGS
|
11月前
|
容器
浅谈SpringMVC之转发与重定向
SpringMVC转发与重定向的使用 转发:由web服务器跳转(可携带数据集合)。 重定向:由浏览器跳转(跳转页面)。
QGS
81 0
Servlet学习——请求跳转和重定向
Servlet学习——请求跳转和重定向
56 0