[OmniFaces utilities] The
getViewMap()
method returns the view scope map.[OmniFaces utilities] The
getViewAttribute()
method returns the view scope attribute value associated with the given name.[OmniFaces utilities] The
setViewAttribute()
method sets the view scope attribute value associated with the given name.[OmniFaces utilities] The
removeViewAttribute()
method removes the view scope attribute value associated with the given name. This method return the view scope attribute value previously associated with the given name, or null
if there is no such attribute.Method Faces#getViewMap()- returns the view scope map
See also: Faces#getContext()
Method Faces#getViewAttribute()- returns the view scope attribute value associated with the given name
Method Faces#setViewAttribute() - sets the view scope attribute value associated with the given name
Method Faces#removeViewAttribute() - removes the view 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 view map (view scope):
import
org.omnifaces.util.Faces;
...
Map<String, Object> viewmap
= Faces.getViewMap();
for (Map.Entry<String,
Object> entry : viewmap.entrySet()) {
System.out.println(entry.getKey() +
"/" + entry.getValue());
}
You can add
an entry (view scope attribute) in the view map (scope) via Faces#setViewAttribute()
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.setViewAttribute(getVar(), something);
If you know
the name of the view scope attribute then you can collect it easily via Faces#getViewAttribute()
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.getViewAttribute("t");
Finally, you
can remove a view scope attribute via Faces#removeViewAttribute() as below
(this method returns the view scope attribute value previously associated with
the given name, or null if there is no such attribute):
// e.g. something
Object something = Faces.removeViewAttribute("t");
Among
others, the view map will contain instances of managed beans that are declared
under the view scope (@ViewScoped (JSF/JSF 2.2 CDI compatible)). For example, let's
suppose that we have this managed bean:
@Named /
@ManagedBean
@ViewScoped
(from javax.faces.view.ViewScoped) / @ViewScoped (from javax.faces.bean.ViewScoped)
public class
LoginBean implements Serializable {
private String email;
private String password;
// getters and setters
}
You can
easily identify such beans by their names which becomes keys in
the request map. Therefore you will be able to locate an
instance of this JSF managed bean in the view map under the key, loginBean.
If you specify the bean name via @Named(value="some_name") or @ManagedBean(name="some_name"), then some_name
will be the key in the view map. So, via the view map, you can access a view
scoped bean property, like this:
String email
= ((LoginBean)(Faces.getViewAttribute("loginBean/some_name"))).getEmail();
Is perfectly
legal to do this also (this refers to the current bean):
@Named(value="some_name")
...
String
bean_name = getClass().getAnnotation(Named.class).value();
String email
= ((LoginBean)(Faces.getViewAttribute(bean_name))).getEmail();
Or like (this
refers to the current bean):
@ManagedBean(name="some_name")
...
String
bean_name = getClass().getAnnotation(ManagedBean.class).name();
String email
= ((LoginBean)(Faces.getViewAttribute(bean_name))).getEmail();
Now, you can
easily intuit how to work with managed beans stored in the view map.
Niciun comentariu :
Trimiteți un comentariu