Flash Attributes
Flash 属性为一个请求提供了一种存储属性的方法,这些属性旨在用于另一个请求中。当重定向(例如,Post-Redirect-Get 模式)时,最常需要这样做。在重定向之前,Flash 属性会暂时保存(通常在会话中),以便在重定向后对请求可用,并且立即将其删除。
Flash attributes provide a way for one request to store attributes that are intended for use in another. This is most commonly needed when redirecting — for example, the Post-Redirect-Get pattern. Flash attributes are saved temporarily before the redirect (typically in the session) to be made available to the request after the redirect and are removed immediately.
Spring MVC 具有两种主要抽象,支持 Flash 属性。FlashMap
用于保存 Flash 属性,而 FlashMapManager
用于存储、检索和管理 FlashMap
实例。
Spring MVC has two main abstractions in support of flash attributes. FlashMap
is used
to hold flash attributes, while FlashMapManager
is used to store, retrieve, and manage
FlashMap
instances.
Flash 属性支持始终为 “处于开启状态”,不需要显式启用。但是,如果不使用它,则它绝不会导致 HTTP 会话创建。对于每个请求,都会有一个 “输入” FlashMap,其中包含从上一个请求(如果存在)传递的属性,以及一个 “输出” FlashMap,其中包含要保存供后续请求使用的属性。这两个 FlashMap 实例都可以通过RequestContextUtils 中的静态方法从 Spring MVC 中的任何位置访问。
Flash attribute support is always “on” and does not need to be enabled explicitly.
However, if not used, it never causes HTTP session creation. On each request, there is an
“input” FlashMap
with attributes passed from a previous request (if any) and an
“output” FlashMap
with attributes to save for a subsequent request. Both FlashMap
instances are accessible from anywhere in Spring MVC through static methods in
RequestContextUtils
.
带注解的控制器通常不需要直接使用 FlashMap
。相反,@RequestMapping
方法可以接受类型为 RedirectAttributes
的参数,并使用它为重定向方案添加 Flash 属性。通过 RedirectAttributes
添加的 Flash 属性会自动传播到 “输出” FlashMap。同样,在重定向之后,来自 “输入” FlashMap 的属性会自动添加到为目标 URL 提供服务的控制器的 Model
。
Annotated controllers typically do not need to work with FlashMap
directly. Instead, a
@RequestMapping
method can accept an argument of type RedirectAttributes
and use it
to add flash attributes for a redirect scenario. Flash attributes added through
RedirectAttributes
are automatically propagated to the “output” FlashMap. Similarly,
after the redirect, attributes from the “input” FlashMap
are automatically added to the
Model
of the controller that serves the target URL.
Flash 属性的概念存在于许多其他 Web 框架中,并且事实证明有时会面临并发问题。这是因为,根据定义,Flash 属性要存储到下一次请求。但是,非常 “下一个” 请求可能不是预期的接收者,而是另一个异步请求(例如,轮询或资源请求),在这种情况下,Flash 属性被删除得太早。
The concept of flash attributes exists in many other web frameworks and has proven to sometimes be exposed to concurrency issues. This is because, by definition, flash attributes are to be stored until the next request. However the very “next” request may not be the intended recipient but another asynchronous request (for example, polling or resource requests), in which case the flash attributes are removed too early.
为了降低此类问题的可能性,RedirectView
会自动使用目标重定向 URL 的路径和查询参数对 FlashMap
实例 “进行标记”。反过来,当默认 FlashMapManager
查找 “输入” FlashMap 时,它会将该信息与传入请求匹配。
To reduce the possibility of such issues, RedirectView
automatically “stamps”
FlashMap
instances with the path and query parameters of the target redirect URL. In
turn, the default FlashMapManager
matches that information to incoming requests when
it looks up the “input” FlashMap
.
这不会完全消除并发问题,但是会极大地降低与重定向 URL 中已经提供的信息相关的此类问题。因此,我们建议主要在重定向场景中使用 Flash 属性。
This does not entirely eliminate the possibility of a concurrency issue but reduces it greatly with information that is already available in the redirect URL. Therefore, we recommend that you use flash attributes mainly for redirect scenarios.