Note that there are some explanatory texts on larger screens.

plurals
  1. POSeveral instances of the same bean
    primarykey
    data
    text
    <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>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. COYou're mixing concepts I think. In your bean A you create both beans but after you rewrite that values with managed property injections, which are probably `null` at that moment. You're also mixing annotations with xml config, be careful with that. I encourage you to use only one of the methods for each bean. About injecting `@ViewScoped` beans in other `@ViewScoped` beans, this will bring you problems. Just inject broader scope beans if you want (`@SessionScoped` in `@ViewScoped` e.g.). If you have common code for two view beans, just implement a plain java class with it and make them extend.
      singulars
    2. COThx for your comment. I am sorry I am beginner and I haven't completely understand your answer. " In your bean A you create both beans but after you rewrite that values with managed property injections, which are probably null at that moment. " In the init method I have new instances of the two beans, so I thought they are not null ? "You're also mixing annotations with xml config" -> may you tell me which annotations ? If I do not declare ma beans in my xml the application doesn't work.
      singulars
    3. COYou're creating your beans by hand, but faces context doesn't have them. To use ` @ManagedProperty(value="#{b2}")` annotations you need them in the context. With annotations I mean you're using `@ManagedProperty` an possibly `@ManagedBean` mixed with the configuration file
      singulars
 

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