heisenberg
heisenberg

Reputation: 1954

JSF Obtaining value from one managed bean to another as argument

I've just created a Login Managed Bean and works perfectly. It takes the value of email and password.

Now, my problem is how to pass the value of the email obtained by the LoginBean.java to my UserBean.java.

index_customer.xhtml

<h:outputText value="#{userBean.firstname}"/>

UserBean.java

@ManagedBean(name="userBean")
public class UserBean {
   private static String firstname;
   private String lastname;
   private String email;
   private String password;

   public String firstname(String email){
       String fname;
       UserDAOImpl userDAOImpl = new UserDAOImpl();
       fname = userDAOImpl.getFirstName(email);
       return fname;
   }  

}

LoginBean.java

@ManagedBean(name = "loginBean")

public class LoginBean {

    private String email;

    public String getEmail() {
        return email;
    }
}

How can i call the method getEmail() and supply the email as argument to UserBean's firstname(email) method?

Is this possible? The reason I asked is, I want to identify the first name of the user who logged in and show something like "Hello YourFirstName" using the EL expression on <h:outputText value="#{userBean.firstname}"/>

I worked with swing before but I know that things are different with web so I guess static variables isn't the correct way to get variable values from one class to another in JSF.

My UserDAOImpl.getFirstName(email) method has an SQL query which is

String SQL = "SELECT firstname from customer WHERE emailcolumn = email;

Thanks in advance. I'd appreciate any help.

Upvotes: 1

Views: 1732

Answers (1)

furkan
furkan

Reputation: 270

Just pass the return value of getEmail to firstName method as parameter.

<h:outputText value="#{userBean.firstname(loginBean.email)}"/>

But of course there are some other details. You should keep this kind of data in a session scoped bean. And DataBase acces on every get is not so healthy.

How to choose the right bean scope?

Upvotes: 1

Related Questions