简体   繁体   English

如何从 JSF 中的 managedBean 重新加载同一页面?

[英]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.我在 JSF 应用程序中实现登录,但重定向有问题。

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.我想在应用程序中的每个 xhtml 中提供登录表单,但是在登录成功或失败后,我想让用户在单击登录时保持在同一页面中。

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.我试图在 managedBean 方法中返回 null 但这不起作用,因为它不会刷新 webPage,我需要刷新页面才能使视图逻辑正常工作。

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:这是 action 属性中的方法:

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"??有一种方法可以返回到用户登录的 xhtml,而不是像“login.xhtml”这样的固定 xhtml?

Just redirect to the request URI .只需重定向请求 URI

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

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

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.您应该通过添加?faces-redirect=true以返回操作值来进行真正的刷新。

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. update 属性将更新组件。

Second solution第二种解决方案

Add ?faces-redirect=true to your return value of action method for a real refresh?faces-redirect=true添加到您的操作方法的返回值以进行真正的刷新

public String login(){

    currentUser = gu.login(usuario, password);

    return "login?faces-redirect=true";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM