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

后端 未结 2 1534
没有蜡笔的小新
没有蜡笔的小新 2020-12-09 13:44

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 th

相关标签:
2条回答
  • 2020-12-09 14:09

    Just redirect to the request URI.

    public void login() throws IOException {
        // ...
    
        ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
        ec.redirect(((HttpServletRequest) ec.getRequest()).getRequestURI());
    }
    
    0 讨论(0)
  • 2020-12-09 14:10

    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";
    }
    
    0 讨论(0)
提交回复
热议问题