Note that there are some explanatory texts on larger screens.

plurals
  1. POSeveral instances of the same bean
    text
    copied!<p>I edit for adding new information : I try to use annotations like you said but it still does not work :</p> <p>My bean A:</p> <pre><code>@ManagedBean(name="abean") @ViewScoped public class ABean { @ManagedProperty(value="#{b1}") private BBean b1; @ManagedProperty(value="#{b2}") private BBean b2; // getter setter } </code></pre> <p>My bean B:</p> <pre><code>@ManagedBean @SessionScoped public class BBean { private String a; private String b; // getter setter public boolean check(){ if (a != null &amp;&amp; !a.isEmpty() &amp;&amp; !a.equals("0")) { return true; } else return false; } } </code></pre> <p>But it still does not work, b1 and b2 are null.</p> <p>But if I name my BBbean, my b1 bean is not null anymore :</p> <pre><code>@ManagedBean(name="b1") @SessionScoped public class BBean { private String a; private String b; // getter setter public boolean check(){ if (a != null &amp;&amp; !a.isEmpty() &amp;&amp; !a.equals("0")) { return true; } else return false; } } </code></pre> <p>So, I have the feeling that it is not possible to have two instances of the same bean ?</p> <hr> <p>Original message : </p> <p>I have several instances of one bean in another bean. I am not able to correctly use these beans.</p> <p>My first bean A :</p> <pre><code>public class ABean { @ManagedProperty(value="#{b1}") private BBean b1; @ManagedProperty(value="#{b2}") private BBean b2; @PostConstruct public void init(){ b1 = new BBean(); b2 = new BBean(); } } </code></pre> <p>My second bean B:</p> <pre><code>public class BBean { private String a; private String b; // getter setter public boolean check(){ if (a != null &amp;&amp; !a.isEmpty() &amp;&amp; !a.equals("0")) { return true; } else return false; } } </code></pre> <p>Both are in scopeView. </p> <p>My faces-config.xml</p> <pre><code>&lt;managed-bean&gt; &lt;managed-bean-name&gt;aBean&lt;/managed-bean-name&gt; &lt;managed-bean-class&gt;com.test.ABean&lt;/managed-bean-class&gt; &lt;managed-bean-scope&gt;view&lt;/managed-bean-scope&gt; &lt;/managed-bean&gt; &lt;managed-bean&gt; &lt;managed-bean-name&gt;b1&lt;/managed-bean-name&gt; &lt;managed-bean-class&gt;com.test.BBean&lt;/managed-bean-class&gt; &lt;managed-bean-scope&gt;view&lt;/managed-bean-scope&gt; &lt;/managed-bean&gt; &lt;managed-bean&gt; &lt;managed-bean-name&gt;b2&lt;/managed-bean-name&gt; &lt;managed-bean-class&gt;com.test.BBean&lt;/managed-bean-class&gt; &lt;managed-bean-scope&gt;view&lt;/managed-bean-scope&gt; &lt;/managed-bean&gt; </code></pre> <p>In my xhtml page I have these two beans like this :</p> <pre><code>#{aBean.b1.check()} #{aBean.b2.check()} </code></pre> <p>The problem is that the check method never return true for b1 even if this one is correctly filled. Is it possible to proceed like I did ? And then to have several instances of only one bean in a parent class ?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload