How to access property of one managed bean in another managed bean

Slay picture Slay · Sep 10, 2012 · Viewed 26k times · Source

I have a managed bean (SessionScope as follow)

@ManagedBean(name="login")
@SessionScoped
public class Login implements Serializable {

   private String userSession;
   public Login(){
   }
}

In this managedbean, somewhere in the login function, i store the email as a session.

I have another managed bean called ChangePassword (ViewScoped). I need to access the value of the email which is stored in the userSession.

The reason of doing so is that i need to find out the current userSession(email) before i can complete the change password function. (Need change password for that specific email)

How do i do so? New to JSF, appreciate any help!

Answer

BalusC picture BalusC · Sep 10, 2012

Just inject the one bean as a managed property of the other bean.

@ManagedBean
@ViewScoped
public class ChangePassword {

    @ManagedProperty("#{login}")
    private Login login; // +setter (no getter!)

    public void submit() {
        // ... (the login bean is available here)
    }

    // ...
}

See also: