Life-time learner's blog

Tag Archives: helper class

JSF Helper Class

When creating web-sites with JSF you often use the same patterns of code. Here are the examples:

// add faces message to Faces Context
FacesContext ctx = FacesContext.getCurrentInstance();
ctx.addMessage(null, new FacesMessage(FacesMessage.FacesMesssage.SEVERITY_INFO,
                                     "Login successful.", "Welcome! You are now signed in."));
// redirect
FacesContext ctx = FacesContext.getCurrentInstance();
NavigationHandler handler = ctx.getApplication().getNavigationHandler();
handler.handleNavigation(ctx, null, "home");
// obtain http session
FacesContext ctx = FacesContext.getCurrentInstance();
HttpServletRequest request = (HttpServletRequest) ctx.getExternalContext().getRequest();
HttpSession session = (HttpSession) request.getSession(true);

This leads to repeating the code and also makes it more difficult to write the tests to the code.
I think it is a good idea to create a helper-class which will do all these operations with faces context under the hood and provide very simple and useful API. Read more of this post