Search code examples
jsfjsf-2parameter-passingmanaged-bean

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


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!


Solution

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