首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Spring : flash属性与模型属性

Spring : flash属性与模型属性
EN

Stack Overflow用户
提问于 2014-09-01 03:56:14
回答 1查看 14.9K关注 0票数 11

flash和模型属性有什么不同?

我希望存储一个对象,并在我的JSP中显示它,以及在其他控制器中重用它。我已经使用了sessionAttribute,它在JSP中工作得很好,但问题是当我试图在其他控制器中检索model属性时。

我失去了一些数据。我四处搜索,发现flash attribute允许不同控制器的过去值,不是吗?

EN

回答 1

Stack Overflow用户

发布于 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实例。

示例

代码语言:javascript
运行
复制
@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");
  }

}

在上面的示例中,请求到达handlePostflashAttributes被添加,并在handleGet方法中检索。

更多信息,这里这里

票数 21
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25598647

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档