joi, 12 martie 2015

[OmniFaces utilities (2.0)] Get/set the Faces context attribute value associated with the given name


[OmniFaces utilities] The getContextAttribute() method returns the Faces context attribute value associated with the given name.
[OmniFaces utilities] The setContextAttribute() method sets the Faces context attribute value associated with the given name.

Methods:


Practically, these two methods allows an easy access to the FacesContext.getAttributes() map. These map is used by JSF to store specific information, but it can be used by the JSF programmers also. You can put here any information that should be available during current context.

Usage:

import org.omnifaces.util.Faces;
...
// add the attribute named, request.date
Faces.setContextAttribute("request.date", new java.util.Date());

Now, the attributes map may look like this:

The key is: com.sun.faces.RESOURCE_REQUEST                    value is :false
The key is: facelets.Encoding                                 value is :UTF-8
The key is: javax.faces.FACELET_CONTEXT                       value is :com.sun.faces.facelets.impl.DefaultFaceletContext@4f17a4cb
The key is: com.sun.faces.renderKitImplForRequest             value is :com.sun.faces.renderkit.RenderKitFactoryImpl@5528c72b
The key is: org.primefaces.context.RequestContext             value is :org.primefaces.context.DefaultRequestContext@1a7e2967
The key is: org.omnifaces.context.OmniPartialViewContext      value is :org.omnifaces.context.OmniPartialViewContext@2fdfa57
The key is: com.sun.faces.INVOCATION_PATH                     value is :.xhtml
The key is: SavedResponseCompleteFlagValue                    value is :false
The key is: request.date                                      value is :Fri Mar 13 08:13:40 EET 2015
The key is: com.sun.faces.logicalViewMap                      value is :-3131292308220084045
The key is: com.sun.faces.actualViewMap                       value is :5309957350313295661
The key is: javax.faces.component.UIViewRoot@5d014bb6         value is :true
...

The OmniFaces, getContextAttribute() can be used to access by name any of these attributes - the above highlighted was extracted below:

import org.omnifaces.util.Faces;
...
// returns false
boolean stage = Faces.getContextAttribute("com.sun.faces.RESOURCE_REQUEST");
// returns Fri Mar 13 08:13:40 EET 2015
Date date = Faces.getContextAttribute("request.date");
// instance of OmniPartialViewContext
OmniPartialViewContext omniPartialViewContext = Faces.getContextAttribute("org.omnifaces.context.OmniPartialViewContext");

Niciun comentariu:

Trimiteți un comentariu