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

3

5

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

<composite:insertChildren />

Which inserts all the component's children there. Is there any way to know whether the component has children? Like a boolean value that could go on a "rendered" attribute.

Preterite answered 13/2, 2011 at 7:59 Comment(0)
T
7

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.

Tarrsus answered 13/2, 2011 at 8:39 Comment(3)
cc.childCount only gives you the right answer, if you don't use <composite:insertChildren /> on the composite implementation.Porphyrin
This does not answer the question per se. Since he wanted the count from <composite:insertChildren /> as noted by @Porphyrin Please see the remarks from wentwog, Brian Leatham or me about how to get the count when this tag is used.Tales
@JohnYeary you're right. Thanks to all of you for submitting those answers. Although the question is technically slightly ambitious (op asked for "the children of the component") it's indeed more logical that the children inserted by <composite:insertChildren /> was meant.Tarrsus
I
6

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.

Ivyiwis answered 3/6, 2011 at 6:39 Comment(2)
Nice find! Gross fix, but nice that you found it ;) For me the syntax that worked was: #{cc.getFacets().get('javax.faces.component.COMPOSITE_FACET_NAME').children}Clisthenes
Since we are looking for the count though, I had to modify it just ever so slightly by adding the size(). #{cc.getFacets().get('javax.faces.component.COMPOSITE_FACET_NAME').children.size()}Tales
A
2

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.

Antipyrine answered 1/3, 2011 at 11:0 Comment(1)
#{component.getCompositeComponentParent(component).childCount} worked for me. Have you tried that?Preterite

© 2022 - 2024 — McMap. All rights reserved.