Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional redirection in JSF

Tags:

redirect

jsf

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.

  1. Add <meta http-equiv="Refresh" content="0; URL=home.jsf" /> and let it be rendered conditionally (EL : #{login.loggedIn})
  2. 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

like image 216
Kai Avatar asked Dec 06 '11 11:12

Kai


1 Answers

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");
    }
}
like image 170
BalusC Avatar answered Sep 28 '22 05:09

BalusC