luni, 13 aprilie 2015

[OmniFaces utilities (2.0)] Returns the HTTP session and creates one if one doesn't exist


[OmniFaces utilities] The getSession() method returns the HTTP session and creates one if one doesn't exist.
[OmniFaces utilities] The getSession(boolean create) method returns the HTTP session and creates one if one doesn't exist and create argument is true, otherwise don't create one and return null.

Note that whenever you absolutely need this methods to perform a general task, you might want to consider to submit a feature request to OmniFaces in order to add a new utility method which performs exactly this general task.

Methods Faces#getSession()/getSession(boolean create) - returns the HTTP session and creates one if one doesn't exist

See also: Faces#getContext()

See also: Faces#getContext()
Usage:

Returns the HTTP session and creates one if one doesn't exist:

import org.omnifaces.util.Faces;
import javax.servlet.http.HttpSession;
...
HttpSession session = Faces.getSession();
HttpSession session = Faces.getSession(true);
// do something with the HttpSession (collected or just created)

Returns the HTTP session if exists, otherwise return null:

import org.omnifaces.util.Faces;
import javax.servlet.http.HttpSession;
...
HttpSession session = Faces.getSession(false);
if(session != null){
   // do something
} else {
   // do something else
}

Niciun comentariu:

Trimiteți un comentariu