My JSF Books/Videos My JSF Tutorials OmniFaces/JSF PPTs
JSF 2.3 Tutorial
JSF Caching Tutorial
JSF Navigation Tutorial
JSF Scopes Tutorial
JSF Page Author Beginner's Guide
OmniFaces 2.3 Tutorial Examples
OmniFaces 2.2 Tutorial Examples
JSF Events Tutorial
OmniFaces Callbacks Usages
JSF State Tutorial
JSF and Design Patterns
JSF 2.3 New Features (2.3-m04)
Introduction to OmniFaces
25+ Reasons to use OmniFaces in JSF
OmniFaces Validators
OmniFaces Converters
JSF Design Patterns
Mastering OmniFaces
Reusable and less-verbose JSF code

My JSF Resources ...

Java EE Guardian
Member of JCG Program
Member MVB DZone
Blog curated on ZEEF
OmniFaces is an utility library for JSF, including PrimeFaces, RichFaces, ICEfaces ...

.

.

.

.

.

.

.

.


[OmniFaces Utilities] - Find the right JSF OmniFaces 2 utilities methods/functions

Search on blog

Petition by Java EE Guardians

Twitter

duminică, 19 aprilie 2015

[OmniFaces utilities (2.0)] Get flash scope map | Get/set/remove a flash scope attribute


[OmniFaces utilities] The getFlash() method returns the flash scope. Note that Flash implements Map<String, Object>, so you can just treat it like a Map<String, Object>.

[OmniFaces utilities] The getFlashAttribute() method returns the flash scope attribute value associated with the given name.
[OmniFaces utilities] The setFlashAttribute() method sets the flash scope attribute value associated with the given name.
[OmniFaces utilities] The removeFlashAttribute() method removes the flash scope attribute value associated with the given name. This method return the flash scope attribute value previously associated with the given name, or null if there is no such attribute.

Method Faces#getFlash()- returns the flash scope.
See also: Faces#getContext()

Method Faces#getFlashAttribute()- returns the flash scope attribute value associated with the given name.
Method Faces#setFlashAttribute()- sets the flash scope attribute value associated with the given name.
Method Faces#removeFlashAttribute()- removes the flash scope attribute value associated with the given name.
See also: Faces#getContext()
Usage:

In JSF, the flash scope is a handy alternative for passing parameters between views by avoiding the session scope. To simply understand it, put it this way: its lifespan spreads over a POST-redirect-GET request, meaning that a variable stored in a flash scope it’s available over ONE JSF redirection and then will be lost. To obtain an instance of flash scope, we can use OmniFaces utility method Faces#getFlash():

import org.omnifaces.util.Faces;
...
Flash flash = Faces.getFlash();

Going further, to add a variable in flash scope we have Faces#setFlashAttribute():

private String car;
private String color;
...
// getters and setters
...
Faces.setFlashAttribute("car", car);
Faces.setFlashAttribute("color", color);

At this moment, these variables will "survive" to ONE redirection. But, if we decide at some point (before they "expire") that we no longer need them, we can simply remove them from the flash scope by using Faces#removeFlashAttribute() - this method returns the flash scope attribute value previously associated with the given name, or null if there is no such attribute:

String car = Faces.removeFlashAttribute("car");
String color = Faces.removeFlashAttribute("color");

Or, we can collect the variable from the flash scope by calling the Faces#getFlashAttribute() method:

String car = Faces.getFlashAttribute("car");
String color = Faces.getFlashAttribute("color");


Note "The @FlashScoped doesn't exist in JSF. The JSF flash scope exist of basically a map which is backed by a short-living cookie which survives HTTP redirects. You cannot let JSF put managed beans in this scope. You've to manually put/get the values in/from the map yourself and/or use the #{flash} reference in EL which basically references the map. Seam Faces has however hijacked the JSF specific javax.faces.bean package for its @FlashScoped annotation, but this is definitely not from standard JSF API." - source: Bauke Scholtz on StackOverflow

Niciun comentariu :

Trimiteți un comentariu

JSF BOOKS COLLECTION

Postări populare

OmniFaces/JSF Fans

Visitors Starting 4 September 2015

Locations of Site Visitors