flash属性与模型属性

val*_*ana 6 attributes spring-mvc

flash和model属性有什么不同?

我想存储一个对象并将其显示在我的jsp中,并在其他控制器中重用它.我有用sessionAttribute,它在jsp中工作正常,但问题是当我尝试model在其他控制器中检索该属性时.

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

Ank*_*hal 10

如果我们想通过attributes via redirect between two controllers,我们不能使用request attributes(他们将无法生存重定向),我们不能使用Spring的@SessionAttributes(因为春节的方式处理它),只是一个普通的HttpSession可以使用,这是不是很方便.

Flash属性为一个请求提供了一种存储打算在另一个请求中使用的属性的方法.重定向时最常需要这种方法 - 例如,Post/Redirect/Get模式.Flash重定向(通常在会话中)之前临时保存Flash属性,以便在重定向后立即删除请求.

Spring MVC有两个主要的抽象支持flash属性.FlashMap用于FlashMapManager存储,检索和管理FlashMap实例时用于保存Flash属性.

@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");
  }

}
Run Code Online (Sandbox Code Playgroud)

在上面的示例中,请求在方法中进行handlePost,flashAttributes添加和重试handleGet.

更多信息在这里这里