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 session scope map | Get/set/remove a session scope attribute


[OmniFaces utilities] The getSessionMap() method returns the session scope map.

[OmniFaces utilities] The getSessionAttribute() method returns the session scope attribute value associated with the given name.
[OmniFaces utilities] The setSessionAttribute() method sets the session scope attribute value associated with the given name.
[OmniFaces utilities] The removeSessionAttribute() method removes the session 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#getSessionMap() - returns the session scope map
See also: Faces#getContext()

Method Faces#getSessionAttribute()- returns the session scope attribute value associated with the given name
Method Faces#setSessionAttribute()- sets the session scope attribute value associated with the given name
Method Faces#removeSessionAttribute()- removes the session 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 session map (session scope):

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

You can add an entry (session scope attribute) in the session map (scope) via Faces#setSessionAttribute() 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.setSessionAttribute(getVar(), something);

If you know the name of the session scope attribute then you can collect it easily via Faces#getSessionAttribute() 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.getSessionAttribute("t");

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

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

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

@Named / @ManagedBean
@SessionScoped (from javax.enterprise.context.SessionScoped) / @SessionScoped  (from javax.faces.bean.SessionScoped)
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 session map. Therefore you will be able to locate an instance of this JSF managed bean in the session 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 session map. So, via the session map, you can access a property of a session scoped JSF managed bean, like this:

String email = ((LoginBean)(Faces.getSessionAttribute("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.getSessionAttribute(bean_name))).getEmail();

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

Niciun comentariu :

Trimiteți un comentariu

JSF BOOKS COLLECTION

Postări populare

OmniFaces/JSF Fans

Visitors Starting 4 September 2015

Locations of Site Visitors