In JSF2, how to know if composite component has children?

前端 未结 3 1103
名媛妹妹
名媛妹妹 2021-01-13 06:32

I\'m writing a composite component, you have a special tag named:


Which inserts all the component\'s chi

相关标签:
3条回答
  • 2021-01-13 06:41

    I've encountered the same problem and managed to find children of a composite component within it's facet 'javax.faces.component.COMPOSITE_FACET_NAME'.

    In Java it's like this:

    // we are within some method of UIComponent
    UIComponent childrenHolderFacet = getFacets().get("javax.faces.component.COMPOSITE_FACET_NAME");
    Iterator<UIComponent> childrenIt = childrenHolderFacet.getChildren().iterator();
    ...
    

    In JSF it's something like:

    #{component.getFacets().get("javax.faces.component.COMPOSITE_FACET_NAME").children}
    

    Hope it helps.

    0 讨论(0)
  • 2021-01-13 06:55

    At least on Mojarra this does not work. A composite component's children get inserted just fine but accessing #{cc.parent} or #{cc.children} does not work on 2.0.2, and #{cc.childCount} always returns 0 on 2.0.4, regardless of the number of children.

    0 讨论(0)
  • 2021-01-13 07:01

    The basic expression you're after is the following:

    #{cc.childCount} or more elaborately:

    #{component.getCompositeComponentParent(component).childCount}

    E.g. the following composite component:

    <html xmlns="http://www.w3.org/1999/xhtml"
        xmlns:h="http://java.sun.com/jsf/html"  
        xmlns:cc="http://java.sun.com/jsf/composite"
    >
        <cc:interface/>
    
        <cc:implementation>             
            <h:outputText value="Children: #{cc.childCount}" />
        </cc:implementation>    
    </html>
    

    used on the following Facelet:

    <html xmlns="http://www.w3.org/1999/xhtml"
        xmlns:h="http://java.sun.com/jsf/html"    
        xmlns:test="http://java.sun.com/jsf/composite/test"    
    >
    
        <h:body>
    
            <test:myCom>
                <h:outputText value="first child" />
                <h:outputText value="second child" />
            </test:myCom>
    
        </h:body>
    </html>
    

    will print Children: 2.

    Thus #{cc.childCount != 0} will tell you whether a composite component has children or not.

    0 讨论(0)
提交回复
热议问题