Spring MVC redirect

来源:互联网 发布:淘宝评价手机怎么修改 编辑:程序博客网 时间:2024/06/08 04:41

Spring MVC重定向方式的页面跳转。重定向分为两种情况,一种是不带参数,一种是带参数。先来看看不带参数的方式:

1. 使用ModelAndView

return new ModelAndView("redirect:/product");

这样就可以把请求重定向到/product路径注解的方法,如:


2. 返回String

return "redirect:/product";

含义同ModelAndView方式,如:


接下来我们看看带参数的方式:

1. 手工拼接

return new ModelAndView("redirect:/productcompany="+boweifeng);

手工拼接有个弊端,传中文可能会有乱码问题。

2. 使用RedirectAttributes自动拼接

 

其原理同手工拼接,RedirectAttribute会把添加的属性转码后附加到URL后。

3. 使用RedirectAttributes,但不拼接URL

 

我们这里使用了addFlashAttribute方法,这样在请求index,跳转到/product URL时,地址栏并不会携带参数。

其原理是,在对请求的重定向生效之前被临时存储(通常是在session中),并且在重定向之后被立即移除。

0 0