Tuesday, March 11, 2008

JSF : Conditional Redirect

How do you redirect based on a condition? In this case, a session variable. Example, whenever a property in the user bean is null, redirect to the login page

Solution

<jsp:scriptlet>




final javax.faces.context.FacesContext facesContext=javax.faces.context.FacesContext.getCurrentInstance();
final com.example.MyBean myBean=(com.example.MyBean)facesContext.getApplication().getVariableResolver().resolveVariable(facesContext, "myBean");
if(myBean.getProperty()==null)
{
facesContext.getExternalContext().redirect(facesContext.getExternalContext().getRequestContextPath()+"/mainmenu.faces");
}

</jsp:scriptlet>

So, put this code in a file and call this by including in whatever page you want! Simple ;)

No comments: