如何将复合组件的动态列表绑定到panelGroup

时间:2013-02-20 13:15:34

标签: jsf

复合组件xhtml:

<composite:interface componentType="metroComponent">
        <composite:attribute name="uniqueId" />
    </composite:interface>


    <composite:implementation>

        <rich:panel width="100%" header="#{msgs['pages.metroEth.header2']}">
            <table class="resData">
                <tr>
                    <td class="labelLeft"><h:outputLabel id="optionLabelId"
                            value="#{msgs['pages.ccparams.serviceOption']}" /></td>
                    <td><h:inputText id="optionTextId"
                            binding="#{cc.serviceOption}" size="15" /> <h:message
                            for="ccvodTextId" style="color:red" /></td>

            </table>
        </rich:panel>

    </composite:implementation>

</ui:composition>

组件类型实现如下:

@FacesComponent (value="metroComponent")
public class HtmlMetroCC extends UIComponentBase implements NamingContainer {

    UIInput serviceOption;

    public UIInput getServiceOption() {
        return serviceOption;
    }

    public void setServiceOption(UIInput serviceOption) {
        this.serviceOption = serviceOption;
    }

    @Override
    public String getFamily() {

        return "javax.faces.NamingContainer";
    }


}

还有一个支持bean,它使用上面列出的复合组件列表来准备panelGroup元素。我删除了getters / setter以便更好地阅读

@ManagedBean (name="metroethernetBean")
@RequestScoped
public class MetroEthernetMBean implements IBean{

    private MetroEthCCData metroCCData;

    private HtmlPanelGroup metroCCPanel;
    private List<HtmlMetroCC> metroCClist;



    @PostConstruct
    public void initBean(){

        metroCClist = new ArrayList<HtmlMetroCC>();
        metroCCPanel = new HtmlPanelGroup();

        HtmlMetroCC initialMetroCC = new HtmlMetroCC(); 
        metroCClist.add(initialMetroCC);

        processMetroCCPanel();

    }


    private void processMetroCCPanel(){
        metroCCPanel.getChildren().clear();

        for (HtmlMetroCC comp: metroCClist){
            metroCCPanel.getChildren().add(comp);
        }   

    }   
}
负责显示panelGroup的

页面片段如下所示:

<h:panelGroup id="metroCCPanelGrouId" binding="#{metroethernetBean.metroCCPanel}" />

问题是,为什么我的复合材料组件没有出现在面板上?它看起来面板没有添加任何孩子..

当我在页面上如下所示:

<gui:metroCC />

然后复合comp。在页面上正确显示。我想要实现的是一个页面上的面板,其中包含复合组件,可以通过单击添加新的动态添加或删除或删除所选的

1 个答案:

答案 0 :(得分:1)

有一个wrokaround,而不是在menaged bean中绑定动态创建的面板

<h:panelGroup id="metroCCPanelGrouId" binding="#{metroethernetBean.metroCCPanel}" />

使用mBean初始化list / add new / remove并在页面上循环遍历:

<h:panelGroup id="metroCCPanelGrouId">
    <ui:repeat var="test" value="#{metroethernetBean.metroCClist}">
        <gui:metroCC binding="#{test}" />
    </ui:repeat>
</h:panelGroup>

但是仍然不明白为什么用我的第一个aproach失败了...... BaluC,你在哪里;)