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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
arg20
  • 4,893
  • 1
  • 50
  • 74

3 Answers3

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.

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
  • 1
    `cc.childCount` only gives you the right answer, if you don't use `` on the composite implementation. – squallsv Sep 11 '14 at 14:10
  • This does not answer the question per se. Since he wanted the count from `` as noted by @squallsv Please see the remarks from wentwog, Brian Leatham or me about how to get the count when this tag is used. – John Yeary Oct 30 '14 at 18:37
  • @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 `` was meant. – Arjan Tijms Oct 30 '14 at 22:04
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.

Artem Nakonechny
  • 121
  • 1
  • 3
  • 4
  • 1
    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}` – Brian Leathem Jul 04 '12 at 00:08
  • 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()}` – John Yeary Oct 30 '14 at 18:33
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.

metasim
  • 4,793
  • 3
  • 46
  • 70
jhhki
  • 37
  • 1
  • #{component.getCompositeComponentParent(component).childCount} worked for me. Have you tried that? – arg20 Mar 01 '11 at 16:56