ViewScope仅实例化一次

时间:2013-02-22 23:03:56

标签: jsf-2 cdi mojarra

我有以下页面,price.xhtml已被包含3次。

<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>

我的price.xhtml使用ViewScope Mbean(priceMBean),问题是,JSF只实例化一个mBean。

我想要3个priceMBean实例,我该如何实现呢?

圣拉斐尔。

1 个答案:

答案 0 :(得分:1)

对于这个典型的用例,最好使用注入而不是在face config

中创建N bean请求范围

BalusC答案很好,可以使用DI和facelets进行改进。

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

如果Child类始终是Parent的依赖类,则可以将Child注释为依赖“@Dependent”。对于依赖,您将有3个不同的Child类实例,它们依赖于主bean(Parent)的生命周期。

您可以使用以下模板:

<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>