如何在JSF中从ManagedBean重新加载同一页面? [英] How can I reload the same page from a managedBean in JSF?

查看:118
本文介绍了如何在JSF中从ManagedBean重新加载同一页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在JSF应用程序中实现登录,并且重定向存在问题.

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

我想在应用程序的每个xhtml中提供登录表单,但登录成功或失败后,我希望将用户保持在与单击登录时相同的页面中.

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.

我试图在managedBean methong中返回null,但这不起作用,因为它不刷新webPage,我需要刷新页面以使视图逻辑起作用.

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.

这是登录表单:

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

这是action属性中的方法:

And this is the method in the action attribute:

public String login(){

    currentUser = gu.login(usuario, password);

    return null;
}

有一种方法可以返回到用户登录的xhtml,而不是像"login.xhtml"一样的固定xhtml?

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

推荐答案

只需请求URI .

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

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

这篇关于如何在JSF中从ManagedBean重新加载同一页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆