You can use the JSF event preRenderView to redirect to another page as follows:
In the index.xhtml file
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core"> <f:view> <ui:insert name="metadata"/> <f:event type="preRenderView" listener="#{yourBean.methodInManagedBean}" /> <h:body></h:body> </f:view> </html>
In a managed bean first way
public class yourClass{ FacesContext fc = FacesContext.getCurrentInstance(); ConfigurableNavigationHandler nav = (ConfigurableNavigationHandler)fc.getApplication().getNavigationHandler(); public void methodInManagedBean() throws IOException { nav.performNavigation("list.do");
or you can use the second method
public class yourClass{ public void methodInManagedBean() throws IOException { FacesContext.getCurrentInstance().getExternalContext().redirect("list.do");
source share