indip: use bean within another bean

I wanted to use a bean within another bean. And I wanted the most elegant way to achieve this.

Here’s what I got:

define the beans in the faces-config.xml as follows (notice the color codings, to show references!)

  <managed-bean>
    <managed-bean-name>childBean</managed-bean-name>
    <managed-bean-class>com.abb.xpages.beans.ChildBean</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
  </managed-bean>
   <managed-bean>
    <managed-bean-name>ParentBean</managed-bean-name>
    <managed-bean-class>com.abb.xpages.beans.ParentBean</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
         <managed-property>
     <property-name>childBeanX</property-name>
     <value>#{childBean}</value>
   </managed-property>
  </managed-bean>

then in your “parentBean” you “inject” the childBean as follows:

ChildBean childBean = null;
public void setChildBeanX(ChildBean bean){
childBean = bean;
}
then you can access the childBean in other places in your parentBean, 

Example method from the parentBean:
public String getColor(){
return childBean.getColor();
}

As always I hope someone might find this useful when struggling around with beans! Also check out my other recent post regarding scope setting if you require notes objects being serialized.

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.