Java 在spring mvc中重定向后从控制器传递参数的方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19266427/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What are ways for pass parameters from controller after redirect in spring mvc?
提问by gstackoverflow
if I write in mycontroller method:
如果我写在 mycontroller 方法中:
return "redirect:url";
What parameters will passes to url(it maybe controler method or jsp page) ?
哪些参数会传递给 url(可能是控制器方法或 jsp 页面)?
采纳答案by Debojit Saikia
With RedirectAttributes
, you can pass almost any data to the redirect URL:
使用RedirectAttributes
,您几乎可以将任何数据传递到重定向 URL:
@RequestMapping(value="/someURL", method=GET)
public String yourMethod(RedirectAttributes redirectAttributes)
{
...
redirectAttributes.addAttribute("rd", "rdValue");
redirectAttributes.addFlashAttribute("fa", faValue);
return "redirect:/someOtherURL";
}
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.
当您使用addAttribute
添加属性时,这将在目标重定向 URL 中结束。这些属性用于构造请求参数,客户端(浏览器)将redirect URL
使用这些参数向 发送新请求。有了这个,你就只能使用 String 或原语作为你的重定向属性。
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).
当您使用 时addFlashAttribute
,这些属性会在重定向之前(通常在会话中)临时保存,并且在重定向之后可用于请求并立即删除。使用的好处flashAttributes
是,您可以将任何对象添加为 flash 属性(因为它存储在会话中)。