使用
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中。这些属性用于构造请求参数,客户端(浏览器)将
redirectURL使用这些参数将新的请求发送到。这样,您就只能使用String或原语作为重定向属性。
并且当您使用时
addFlashAttribute,这些属性会在重定向之前(通常在会话中)临时保存,并在重定向后可供请求使用并立即删除。使用的好处
flashAttributes是,您可以将任何对象添加为flash属性(因为它存储在会话中)。



