在spring mvc中重定向后从控制器传递参数的方法是什么? [英] What are ways for pass parameters from controller after redirect in spring mvc?

查看:27
本文介绍了在spring mvc中重定向后从控制器传递参数的方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在 mycontroller 方法中写:

if I write in mycontroller method:

return  "redirect:url";

什么参数会传递给url(可能是controler方法或jsp页面)?

What parameters will passes to url(it maybe controler method or jsp page) ?

推荐答案

使用 RedirectAttributes,您几乎可以将任何数据传递到重定向 URL:

With RedirectAttributes, you can pass almost any data to the redirect URL:

@RequestMapping(value="/someURL", method=GET)
public String yourMethod(RedirectAttributes redirectAttributes)
{
   ...
   redirectAttributes.addAttribute("rd", "rdValue");
   redirectAttributes.addFlashAttribute("fa", faValue);
   return "redirect:/someOtherURL";
}

当您使用 addAttribute 添加属性时,这将在目标重定向 URL 中结束.这些属性用于构造请求参数,客户端(浏览器)将使用这些参数向 redirect URL 发送新请求.有了这个,您就只能使用字符串或原语作为重定向属性.

When you use addAttribute to add attributes, this will end up in the target redirect URL. These attributes are used to construct the request parameters and the client (browser) will send a new request to the redirect URL with these parameters. With this, you are limited to use String or primitives as your redirect attributes.

当你使用 addFlashAttribute 时,这些属性会在重定向前临时保存(通常在会话中),重定向后可用于请求并立即删除.使用 flashAttributes 的好处是,您可以添加任何对象作为 flash 属性(因为它存储在会话中).

And when you use addFlashAttribute, these attributes are saved temporarily before the redirect (typically in the session) and are available to the request after the redirect and removed immediately. The advantage of using flashAttributes is that, you can add any object as a flash attribute (as it is stored in session).

这篇关于在spring mvc中重定向后从控制器传递参数的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆