[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