在Spring MVC中重定向后从控制器传递参数的方法是什么?
问题内容:
如果我用mycontroller方法编写:
return "redirect:url";
哪些参数将传递给url(可能是控制方法或jsp页面)?
问题答案:
使用RedirectAttributes
,您几乎可以将任何数据传递到重定向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
使用这些参数将新的请求发送到。这样,您就只能使用String或原语作为重定向属性。
并且当您使用时addFlashAttribute
,这些属性会在重定向之前(通常在会话中)临时保存,并在重定向后可供请求使用并立即删除。使用的好处flashAttributes
是,您可以将任何对象添加为flash属性(因为它存储在会话中)。