Adding flash scope variables (JSF 2.0)

You can pass temporary objects between views using flash scope variables. Flash scope is useful if you want to enter data on a page and have the data available for the next request, but not for subsequent requests.

About this task

Flash scope lasts longer than the request scope, but is shorter than the session scope. An advantage of using flash scope over session scope is that flash scope automatically clears out the value once the second view is completed, whereas session scope requires you to manually remove the value.

Procedure

  1. In the Page Data view, right-click the Flash Variables node and select New > Flash Scope Variable.
  2. In the Name field, enter the name of the variable.
  3. In the Type field, enter a Java type or click the ... button to open the Choose Java Type dialog box where you can enter the first few characters of the type you want to select.
  4. Click OK.

Results

The variable appears in the Page Data view under the Flash Variables node and is available to all Facelet pages in this web project.

What to do next

You can use the flash variable to insert data controls by dragging and dropping the variable on your page, or by right-clicking the variable in the Page Data view and selecting Insert New Controls for variable_name. When you drop the controls on your page, the variable appears in the value attribute on the component that you dropped, for example<h:outputText styleClass="outputText" id="textContent1" value="#{flash.myBean.textContent}"></h:outputText>
Note: If you want the values in the flash variable to persist for another post-redirect-get cycle, use the keep keyword, for example#{flash.keep.myBean.textContent}. You can use the ExternalContext.getFlash() API to access your flash variable programmatically in an action method, or you can use EL expressions to access the values in your flash variables.

Feedback