[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