Passing parameters between managedbeans primefaces

12,613

BalusC wrote a whole blog post about communication in JSF 2.0, it is truly worthy of your time. Reading it, you will discover that there are more than one way of doing it, one of them being injecting the property itself, in your other beans:

@ManagedProperty("#{loginBean.fullName}")
private String fullName;

And another, perhaps more appropriate, could be to inject the bean itself:

@ManagedProperty("#{loginBean}")
private LoginBean loginBean;
Share:
12,613
Ali Yucel Akgul
Author by

Ali Yucel Akgul

Updated on November 23, 2022

Comments

  • Ali Yucel Akgul
    Ali Yucel Akgul over 1 year

    I have a problem with my managedbeans. I cannot manage to pass parameters between them. Here is an XHTML snippet. It is basically a form for login. It just sends the parameters to back bean.

    <h:outputLabel for="username" value="Kullanıcı Adı: *" />
      <p:inputText id="username" value="#{loginBean.username}" required="true" requiredMessage="Kullanıcı adı giriniz.">
        <f:validateLength minimum="2" />
      </p:inputText>
      <p:message for="username" display="icon"/>
      <h:outputLabel for="password" value="Şifre: *" />
      <p:inputText id="password" value="#{loginBean.password}" required="true" requiredMessage="Şifreyi giriniz!" type="password">
        <f:validateLength minimum="2" />
      </p:inputText>
      <p:message for="password" id="msgPass" display="icon"/>
      <f:facet name="footer">
        <center>
          <p:commandButton id="submit" value="Giriş" icon="ui-icon-check" action="#{loginBean.check}" style="margin:0" update="grid"/>
        </center>
      </f:facet>
    

    In my backing bean, I am checking whether the user input matches with the database record. If so then I let him enter the system. At the same time, I am taking his full name.

    My backbean:

    @ManagedBean
    @RequestScoped
    public class LoginBean {
      public String getUsername() {
        return username;
      }
      public void setUsername(String username) {
        this.username = username;
      }
      public String getPassword() {
        return password;
      }
      public void setPassword(String password) {
        this.password = password;
      }
      public String getMgs() {
        return mgs;
      }
      public void setMgs(String mgs) {
        this.mgs = mgs;
      }
      public String getFullname() {
        return fullname;
      }
      public void setFullname(String fullname) {
        this.fullname = fullname;
      }
      public String getOriginalURL() {
        return originalURL;
      }
      public void setOriginalURL(String originalURL) {
        this.originalURL = originalURL;
      }
      private String username;
      private String password;
      private String mgs;
      private String fullname;
      private String originalURL;
      private static Logger log = Logger.getLogger(LoginBean.class.getName());
      public String check() throws Exception {
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/projetakip", "root", "");
        Statement stmt = con.createStatement();
        String md5Pass = md5(password);
        String SQL = "select * from users where username='" + username + "' and password='" + md5Pass + "'";
        ResultSet rs = stmt.executeQuery(SQL);
        while (rs.next()) {
          if (username.matches(rs.getString("username")) && md5Pass.matches(rs.getString("password"))) {
            this.fullname = rs.getString("ad") + " " + rs.getString("soyad");
            return "panel?faces-redirect=true";
          } else {
            FacesMessage msg = new FacesMessage("Yanlış kullanıcı adı/şifre.");
            FacesContext.getCurrentInstance().addMessage(null, msg);
            return "index?faces-redirect=true";
          }
        }
        return "index?faces-redirect=true";
      }
      public void getProductSetupData(ActionEvent event) {
        FacesContext context = FacesContext.getCurrentInstance();
        Data data = context.getApplication().evaluateExpressionGet(context, "#{data}", Data.class);
      }
    

    What I want is to pass fullName to other beans (or pages). How can I pass this variable between my beans?

    Thanks in advance.

  • Fritz
    Fritz almost 8 years
    hi. your link is broken
  • Alfian Nahar
    Alfian Nahar almost 8 years
    @Fritz thanks, it worked for me redirecting to: balusc.omnifaces.org/2011/09/communication-in-jsf-20.html, so I changed to this location
  • Fritz
    Fritz almost 8 years
    i see. tried also on my home pc. must be the proxy at work. thanks! :D