A FlashScope is an object that can be used to store objects and make them available as request parameters during this request cycle and the next one. It is extremely useful when implementing the redirect-after-post pattern in which an ActionBean receives a POST, does some processing and then redirects to a JSP to display the outcome. FlashScopes make temporary use of session to store themselves briefly between two requests.
In general, use of the FlashScope should be intermediated by the {@link net.sourceforge.stripes.action.ActionBeanContext}, making it transparent to the rest of the application. Any object that is put into a FlashScope will be immediately exposed in the current request as a request attribute, and under certain conditions will also be exposed in the subsequent request as a request attribute.
To make values available to the subsequent request a parameter must be included in the redirect URL that identifies the flash scope to use (this avoids collisions where two concurrent requests in the same session might otherwise cause problems for one another). The Stripes {@link net.sourceforge.stripes.action.RedirectResolution} will automaticallyinsert this parameter into the URL when a flash scope is present. Should you wish to issue redirects using a different mechanism you will need to add the parameter using code similar to the following:
FlashScope flash = FlashScope.getCurrent(request, false); if (flash != null) { url.addParameter(StripesConstants.URL_KEY_FLASH_SCOPE_ID, flash.key()); }
The lifecycle of a FlashScope is managed is conjunction with the {@link StripesFilter}. FlashScopes are manufactured using lazy instantiation when {@code FlashScope.getCurrent(request, true)} is called. When a request is completed, theStripesFilter notifies the current FlashScope that the request is over, which causes it to record the time when the request terminated. On the subsequent request, if the flash scope is referenced by a URL parameter, then it is removed from session and it's contents are pushed into request attributes for the current request.
To ensure that orphaned FlashScopes do not consume increasing amounts of HttpSession memory, the StripesFilter, after each request, checks to see if any FlashScopes have recently expired. A FlashScope is expired when the length of time from the end of the request that created the FlashScope is greater than the timeout set on the FlashScope. The default timeout is 120 seconds (or two minutes), and can be varied by calling {@link #setTimeout(int)} Since the timerstarts when a request completes, and FlashScopes are only meant to live from the end of one request to the beginning of a subsequent request this value is set quite low.
@author Tim Fennell
@since Stripes 1.2