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

查看:384
本文介绍了在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 ,您几乎可以将任何数据传递到重定向网址:

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 发送新请求。有了这个,你只能使用String或原语作为你的重定向属性。

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天全站免登陆