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

miercuri, 22 aprilie 2015

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


[OmniFaces utilities] The getApplicationMap() method returns the application scope map.

[OmniFaces utilities] The getApplicationAttribute() method returns the application scope attribute value associated with the given name.
[OmniFaces utilities] The setApplicationAttribute() method sets the application scope attribute value associated with the given name.
[OmniFaces utilities] The removeApplicationAttribute() method removes the application scope attribute value associated with the given name. This method return the session scope attribute value previously associated with the given name, or null if there is no such attribute.

Method Faces#getApplicationMap()- returns the application scope map
See also: Faces#getContext()

Method Faces#getApplicationAttribute()- returns the application scope attribute value associated with the given name
Method Faces#setApplicationAttribute()- sets the application scope attribute value associated with the given name
Method Faces#removeApplicationAttribute()- removes the application scope attribute value associated with the given name
See also: Faces#getContext()
Usage:

Below you can see an example of listing the content of the application map (application scope):

import org.omnifaces.util.Faces;
...
Map<String, Object> applicationmap = Faces.getApplicationMap();
for (Map.Entry<String, Object> entry : applicationmap.entrySet()) {
     System.out.println(entry.getKey() + "/" + entry.getValue());
}

You can add an entry (application scope attribute) in the application map (scope) via Faces#setApplicationAttribute() method. For example, you may need to store something under a key representing a variable name provided by the JSF page author using a var like attribute.
...
private enum PropertyKeys {
  var;
}
...
public String getVar() {
  // return "var" value from state
}

Faces.setApplicationAttribute(getVar(), something);

If you know the name of the application scope attribute then you can collect it easily via Faces#getApplicationAttribute() method. Suppose that the variable name (returned by getVar()), is t. Then, we can obtain the something stored under t like this:

// e.g. something
Object something = Faces.getApplicationAttribute("t");

Finally, you can remove a application scope attribute via Faces#removeApplicationAttribute() as below (this method returns the application scope attribute value previously associated with the given name, or null if there is no such attribute):

// e.g. something
Object something = Faces.removeApplicationAttribute("t");

Among others, the application map will contain instances of managed beans that are declared under the application scope (@ApplicationScoped (JSF/CDI)). For example, let's suppose that we have this managed bean:

@Named / @ManagedBean
@ApplicationScoped (from javax.enterprise.context.ApplicationScoped) / @ApplicationScoped  (from javax.faces.bean.ApplicationScoped)
public class LoginBean implements Serializable {

 private String email;    
 private String password; 

 // getters and setters
}

In case of JSF managed beans (not CDI managed beans - in this case, the keys are pretty complex), you can easily identify such beans by their names which becomes keys in the application map. Therefore you will be able to locate an instance of this JSF managed bean in the application map under the key, loginBean. If you specify the bean name via @ManagedBean(name="some_name"), then some_name will be the key in the application map. So, via the application map, you can access a property of a application scoped JSF managed bean, like this:

String email = ((LoginBean)(Faces.getApplicationAttribute("loginBean/some_name"))).getEmail();

Is perfectly legal to do this also (this refers to the current bean):

@ManagedBean(name="some_name")
...
String bean_name = getClass().getAnnotation(ManagedBean.class).name();
String email = ((LoginBean)(Faces.getApplicationAttribute(bean_name))).getEmail();

Now, you can easily intuit how to work with managed beans stored in the application map.

Niciun comentariu :

Trimiteți un comentariu

JSF BOOKS COLLECTION

Postări populare

OmniFaces/JSF Fans

Follow by Email

Visitors Starting 4 September 2015

Locations of Site Visitors