title: how to access one managedbean from another in jsf
body: <p>I have two managedbean class(<code>UserBean.java</code> &amp; <code>HelloBean.java</code>) and one <code>index.xhthl</code> class. In <code>HelloBean.java</code> class, I have used <code>@ManagedProperty</code> anotation for accessing the property of <code>UserBean.java</code> and  I have a method within <code>HelloBean.java</code> class.</p>
  
  <p>here, my classes:</p>
  
  <p><strong>UserBean.java</strong></p>
  
  <pre><code>package com.bean;
  
  import javax.faces.bean.ManagedBean;
  import javax.faces.bean.SessionScoped;
  
  
  @ManagedBean(name="ubean", eager=true)
  @SessionScoped
  public class UserBean {
  
      private String username;
  
      public String getUsername() {
          return username;
      }
      public void setUsername(String username) {
          this.username = username;
      }   
  }
  </code></pre>
  
  <p><strong>HelloBean.java</strong></p>
  
  <pre><code>package com.bean;
  
  import javax.faces.bean.ManagedBean;
  import javax.faces.bean.ManagedProperty;
  import javax.faces.bean.RequestScoped;
  
  
  @ManagedBean(name="hellobean", eager=true)
  @RequestScoped
  public class HelloBean {
  
      @ManagedProperty(value="#{ubean}")
      private UserBean mbean;
      private String username;
  
      public String getUsername() {
          if(mbean!=null){
              username=mbean.getUsername();
          }
          return username;
      }
  
      public void setMbean(UserBean mbean) {
          this.mbean = mbean;
      }
  
      public UserBean getMbean() {
          return mbean;
      }
  
      public void showMsg(){  
          System.out.println("UserName :"+username);
      }
  }
  </code></pre>
  
  <p>And, <strong>index.xhtml</strong></p>
  
  <pre><code>&lt;body&gt;
      &lt;h:form&gt;
          &lt;h:inputText id="username" value="#{ubean.username}"&gt;&lt;/h:inputText&gt;
          &lt;h:commandButton value="submit" action="#{hellobean.showMsg}"&gt;&lt;/h:commandButton&gt;
      &lt;/h:form&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p>I want to invoke the <code>showMsg()</code> method from <code>index.xhtml</code> class. The method is fired by clicking <code>commandButton</code>, but it always returns <code>null</code> instead of <code>inputText</code> value. What's the problem in my codes. Someone, helps...
  Thanks in advance..</p>