1

I have the following page, the price.xhtml has been included 3 times.

<f:subview id="include">
     <ui:include src="/secure/menu/price.xhtml">
        <ui:param name="info" value="#{msg['menu.step3.header']}" />
        <ui:param name="domainKey" value="KEY1" />
     </ui:include>
 </f:subview>
     <f:subview id="include2">
     <ui:include src="/secure/menu/price.xhtml">
        <ui:param name="info" value="#{msg['menu.step3.header']}" />
        <ui:param name="domainKey" value="KEY3" />
     </ui:include>
 </f:subview>
     <f:subview id="include2">
     <ui:include src="/secure/menu/price.xhtml">
        <ui:param name="info" value="#{msg['menu.step3.header']}" />
        <ui:param name="domainKey" value="KEY2" />
     </ui:include>
 </f:subview>

My price.xhtml utilize a ViewScope Mbean (priceMBean), the problem is, JSF only instantiate a single mBean.

I want 3 instances of the priceMBean, how do i achieve this ?

Raphael.

Raphael
  • 11
  • 1
  • 2

1 Answers1

1

For this typical use case it is better to use injection instead of creating N bean requested scope in the face config

BalusC answers is good and can be improved with DI and facelets.

@ManagedBean
@ViewScoped
public class Parent {
    @Inject
    private Child price1;
    @Inject
    private Child price2;
    @Inject
    private Child price3;
    // ...
}

If Child class is always a dependent class of Parent, Child can be annotated as dependent "@Dependent". With dependent, you will have 3 distinct instances of Child class dependent of the lifecycle of the main bean (Parent).

And you can use the following template:

<f:subview id="include">
    <ui:include src="/secure/menu/price.xhtml">
        <ui:param name="info" value="#{msg['menu.step3.header']}"/>
        <ui:param name="domainKey" value="KEY1"/>
        <ui:param name="price" value="#{priceMBean.price1}"/>
    </ui:include>
</f:subview>
<f:subview id="include2">
    <ui:include src="/secure/menu/price.xhtml">
        <ui:param name="info" value="#{msg['menu.step3.header']}"/>
        <ui:param name="domainKey" value="KEY3"/>
        <ui:param name="price" value="#{priceMBean.price2}"/>
    </ui:include>
</f:subview>
<f:subview id="include2">
    <ui:include src="/secure/menu/price.xhtml">
        <ui:param name="info" value="#{msg['menu.step3.header']}"/>
        <ui:param name="domainKey" value="KEY2"/>
        <ui:param name="price" value="#{priceMBean.price3}"/>
    </ui:include>
</f:subview>
Nicolas Labrot
  • 4,017
  • 25
  • 40