6

Im implementing a login in a JSF application and have a problem with the redirection.

I want to make available the login form in every xhtml in the app, but after the login success or fail I want to keep the user in the same page they were when clicked on login.

I have tried to return null in the managedBean methong but that doesnt work because it not refreshes the webPage, I need the page to be refreshed for the view logic to work.

This is the login form:

<h:form id="loginForm" rendered="#{!loginBean.estaLogueado()}">
                    <p:panel header="#{msg.header_login}">
                        <h:outputLabel for="login" value="#{msg.login}"/>
                        <p:inputText id="login" value="#{loginBean.usuario}"></p:inputText><br/>
                        <h:outputLabel for="pwd" value="#{msg.password}"/>
                        <p:inputText id="pwd" type="password" value="#{loginBean.password}"></p:inputText><br/>
                        <p:commandButton action="#{loginBean.login()}" value="Login"/>
                    </p:panel>
                </h:form>
                <h:form id="logoutForm" rendered="#{loginBean.estaLogueado()}">

                    Bienvenido #{loginBean.nombreUsuario}!!<br/>

                    <p:commandButton action="#{loginBean.logout()}" value="Desconectar"/>

                </h:form>

And this is the method in the action attribute:

public String login(){

    currentUser = gu.login(usuario, password);

    return null;
}

There is a way to return to the xhtml where the user loged, not being a fixed xhtml like "login.xhtml"??

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Gustach
  • 63
  • 1
  • 1
  • 7

2 Answers2

27

Just redirect to the request URI.

public void login() throws IOException {
    // ...

    ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
    ec.redirect(((HttpServletRequest) ec.getRequest()).getRequestURI());
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Isn't it best practice to return the page to redirect to in the action field? (in this instance, you could change the action to actionListener) – Steven Jan 29 '14 at 10:09
  • 1
    @Steven: 1) OP can't hardcode the outcome value (this action can be called from multiple pages) thus current page has to be resolved programmatically. 2) Action listeners aren't intented for business logic. – BalusC Jan 29 '14 at 11:20
  • 1
    How can I preserve a `` of the originally called from page? `getQueryString()` returns `null`... – jansohn Oct 30 '15 at 10:51
  • @BalusC How can I reload browser cache while redirecting to a page from its backing bean? – Vombat Oct 28 '17 at 11:19
6

As far as i am concern there is 2 ways for this purpose.

  1. You should define the components which need to be updated in update attribute of caller commandButton.
  2. You should do a real refresh by adding ?faces-redirect=true to return value of action.

First solution.

<h:form id="loginForm" rendered="#{!loginBean.estaLogueado()}">
                    <p:panel header="#{msg.header_login}">
                        <h:outputLabel for="login" value="#{msg.login}"/>
                        <p:inputText id="login" value="#{loginBean.usuario}"></p:inputText><br/>
                        <h:outputLabel for="pwd" value="#{msg.password}"/>
                        <p:inputText id="pwd" type="password" value="#{loginBean.password}"></p:inputText><br/>
                        <p:commandButton action="#{loginBean.login()}" value="Login" update=":loginForm :logoutForm"/>
                    </p:panel>
                </h:form>
                <h:form id="logoutForm" rendered="#{loginBean.estaLogueado()}">

                    Bienvenido #{loginBean.nombreUsuario}!!<br/>

                    <p:commandButton action="#{loginBean.logout()}" update=":loginForm :logoutForm" value="Desconectar"/>

                </h:form>

the update attribute will update the components.

Second solution

Add ?faces-redirect=true to your return value of action method for a real refresh

public String login(){

    currentUser = gu.login(usuario, password);

    return "login?faces-redirect=true";
}
erencan
  • 3,725
  • 5
  • 32
  • 50