Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
575 views
in Technique[技术] by (71.8m points)

redirect - How can I reload the same page from a managedBean in JSF?

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"??

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Just redirect to the request URI.

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

    ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
    ec.redirect(((HttpServletRequest) ec.getRequest()).getRequestURI());
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...