Remove a bean from the session
In the application I'm building there's this "User" bean that holds all kinds of user data and provides some application logic. When the user clicks the log out button, it would be neat to cleanup that bean. The following line of SSJS code removes the "User" bean.
facesContext.getExternalContext().getSessionMap().remove("User");Invalidate the session
Another option for the log-out cleanup function is to invalidate the whole session, which you can do with the following line of SSJS code:
facesContext.getExternalContext().getSession(true).invalidate();
Of course, this cleans up a lot more than just the User bean.
Read property from resource bundle
In the beans, you would of course like to use the resource files. It's easy. Put the following Java method in a Utils class:
public static String getResource(String bundleVar, String name) {
XSPContext xspContext = XSPContext.getXSPContext(FacesContext
.getCurrentInstance());
try {
// There has to be bundle defined with var name
ResourceBundle bundle;
bundle = xspContext.bundle(bundleVar);
if (bundle != null) {
return bundle.getString(name);
}
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
And call it as follows:
Utils.getResource("settings", "db_users");For reference, "settings" is the name of the bundle resource and "db_users" is the name of the setting. From the theme configuration:
/WEB-INF/settings.properties settings
Get a request variable
The User bean does some auto-login magic. To be able to do that it needs to read the request cookies. No problem! Take this static function, put it in some Utils class:
Utils.getCookies();
public static String getCookies() {
// System.out.print(XSPContext.getXSPContext(
// FacesContext.getCurrentInstance()).getUrl());
HttpServletRequest request = (HttpServletRequest) FacesContext
.getCurrentInstance().getExternalContext().getRequest();
return request.getHeader("cookie");
}
You can modify this to read other request parameters, to extend the handling of cookies, etc..
Have fun!
Just a minor note. Seems that there isn't currently available ViewMap. e.g. you cannot use something like
ReplyDeletefacesContext.getExternalContext().getViewMap().remove("User");