flash和模型属性有什么不同?
我希望存储一个对象,并在我的JSP中显示它,以及在其他控制器中重用它。我已经使用了sessionAttribute
,它在JSP中工作得很好,但问题是当我试图在其他控制器中检索model
属性时。
我失去了一些数据。我四处搜索,发现flash attribute
允许不同控制器的过去值,不是吗?
发布于 2014-09-01 04:24:41
如果我们想要传递attributes via redirect between two controllers
,就不能使用request attributes
(它们不能在重定向中生存),也不能使用Spring的@SessionAttributes
(因为Spring处理它的方式),只能使用普通的HttpSession
,这不太方便。
闪存属性为一个请求提供了一种方式来存储要在另一个请求中使用的属性。在重定向时,这是最常用的--例如,Post/ redirecting /Get模式。在重定向之前(通常在会话中),在重定向和立即删除请求之前暂时保存Flash属性。
Spring有两个支持flash属性的主要抽象。FlashMap
用于保存闪存属性,而FlashMapManager
用于存储、检索和管理FlashMap
实例。
示例
@Controller
@RequestMapping("/foo")
public class FooController {
@RequestMapping(value = "/bar", method = RequestMethod.GET)
public ModelAndView handleGet(Model model) {
String some = (String) model.asMap().get("some");
// do the job
}
@RequestMapping(value = "/bar", method = RequestMethod.POST)
public ModelAndView handlePost(RedirectAttributes redirectAttrs) {
redirectAttrs.addFlashAttribute("some", "thing");
return new ModelAndView().setViewName("redirect:/foo/bar");
}
}
在上面的示例中,请求到达handlePost
,flashAttributes
被添加,并在handleGet
方法中检索。
更多信息,这里和这里。
https://stackoverflow.com/questions/25598647
复制相似问题