Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 461
Gustach Avatar asked May 10 '13 13:05

Gustach


People also ask

What is managed bean in JSF?

Managed Bean is a regular Java Bean class registered with JSF. In other words, Managed Beans is a Java bean managed by JSF framework Managed bean contains the getter and setter methods, business logic, or even a backing bean Managed beans works as Model for UI component. Managed Bean can be accessed from JSF page.

How to implement request scoped beans in JSF?

Just put the desired logic in the constructor of the request scoped bean associated with the JSF page. public Bean () { // Do your stuff here. } Use @PostConstruct annotated method on a request or view scoped bean. It will be executed after construction and initialization/setting of all managed properties and injected dependencies.

What is @ManagedBean in Salesforce?

@ManagedBean marks a bean to be a managed bean with the name specified in name attribute. If the name attribute is not specified, then the managed bean name will default to class name portion of the fully qualified class name. In our case, it would be helloWorld.

How to use @PostConstruct in @JSF when Bean created?

JSF 2.2 has a new feature which performs this task using viewAction. Show activity on this post. @PostConstruct is run ONCE in first when Bean Created. the solution is create a Unused property and Do your Action in Getter method of this property and add this property to your .xhtml file like this :


2 Answers

Just redirect to the request URI.

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

    ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
    ec.redirect(((HttpServletRequest) ec.getRequest()).getRequestURI());
}
like image 64
BalusC Avatar answered Oct 10 '22 21:10

BalusC


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";
}
like image 6
erencan Avatar answered Oct 10 '22 21:10

erencan