Conditional redirection in JSF
jsf, redirect
Solution
You could use `<f:event type="preRenderView">` for this.
E.g.
<f:event type="preRenderView" listener="#{login.checkAlreadyLoggedin}" />
with
public void checkAlreadyLoggedin() throws IOException {
if (isLoggedIn()) {
ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
ec.redirect(ec.getRequestContextPath() + "/home.xhtml");
}
}
Problem
is there a build-in mechanism to conditionally redirect to another view? I want the user to be redirected from the login page to the "home page" if he/she is already logged in. I already have two basic approaches, but for the first I have no idea how to achieve and the second is sort of a dirty workaround. - Add `<meta http-equiv="Refresh" content="0; URL=home.jsf" />` and let it be rendered conditionally (EL : `#{login.loggedIn}`) - Add a `<h:panelGroup />` which will also be conditionally rendered, containing some JavaScript doing the redirect. Is there a way to achieve 1 or even another, more elegant solution? :-) Thanks