[OmniFaces utilities] The
getRequestMap()
method returns the request scope map.[OmniFaces utilities] The
getRequestAttribute()
method returns the request scope attribute value associated with the given name.[OmniFaces utilities] The
setRequestAttribute()
method sets the request scope attribute value associated with the given name.[OmniFaces utilities] The
removeRequestAttribute()
method removes the request scope attribute value associated with the given name. This method return the request scope attribute value previously associated with the given name, or null
if there is no such attribute.Method Faces#getRequestMap() - returns the request scope map.
See also: Faces#getContext()
Method Faces#getRequestAttribute()- returns the request scope attribute value associated with the given name.
Method Faces#setRequestAttribute()- sets the request scope attribute value associated with the given name.
Method Faces#removeRequestAttribute()- removes the request 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 request map (request scope):
import
org.omnifaces.util.Faces;
...
Map<String, Object> requestmap
= Faces.getRequestMap();
for
(Map.Entry<String, Object> entry : requestmap.entrySet()) {
System.out.println(entry.getKey() +
"/" + entry.getValue());
}
And a
possible output (beside entries like those listed below, in this map you will find instances of managed beans that are under request scope (@RequestScoped), if there are any):
org.jboss.weld.context.ConversationContext.conversations/{}
org.jboss.weld.servlet.ConversationContextActivatorCONTEXT_ACTIVATED_IN_REQUEST/true
org.jboss.weld.context.ignore.guard.marker/java.lang.Object@2274bb86
com.sun.faces.context.ExternalContextFactoryImpl_KEY/com.sun.faces.context.ExternalContextImpl@215690a1
...
...
You can add
an entry (request scope attribute) in the request map (scope) via Faces#setRequestAttribute()
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.setRequestAttribute(getVar(),
something);
If you know
the name of the request scope attribute then you can collect it easily via Faces#getRequestAttribute()
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.getRequestAttribute("t");
Finally, you
can remove a request scope attribute via Faces#removeRequestAttribute()
as below (this method returns the request scope attribute value previously
associated with the given name, or null if there is no such attribute):
// e.g. something
Object something = Faces.removeRequestAttribute("t");
Among others, the request map will contain instances of managed beans that are declared under the request scope (@RequestScoped (JSF/CDI)). For example, let's suppose that we have this managed bean:
Among others, the request map will contain instances of managed beans that are declared under the request scope (@RequestScoped (JSF/CDI)). For example, let's suppose that we have this managed bean:
@Named /
@ManagedBean
@RequestScoped
(from javax.enterprise.context.RequestScoped) / @RequestScoped (from javax.faces.bean.RequestScoped)
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 request map. Therefore you
will be able to locate an instance of this JSF managed bean in the request 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 request map. So, via the request map, you can access a property of a request scoped JSF managed bean, like this:
String email
= ((LoginBean)(Faces.getRequestAttribute("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.getRequestAttribute(bean_name))).getEmail();
Now, you can
easily intuit how to work with managed beans stored in the request map.
Niciun comentariu :
Trimiteți un comentariu