Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSF2 composite components: are #{cc.childCount} and <composite:insertChildren/> mutually exclusive?

I just dont get it: If I want my composite component to insert children, I use <composite:insertChildren/> but #{cc.childCount} always returns 0 in that case. On the other hand, If I do not use <composite:insertChildren/> I always get correct childCount without children being rendered. Why is that happening?

All I want to do in my component is to render some "default" panel if there are no children and do not render it in other case - behavior similar to <ui:insert name="param_name">default value</ui:insert>. So I need both insertChildren and childCount which do not seem to work together.

Here is the code:

<my:test>
  <h:outputText value="child1" rendered="#{some.trueValue}"/>
  <h:outputText value="child2" rendered="#{some.trueValue}"/>
<my:test>

If I use implementation below, I get 2 rendered as expected

<composite:implementation>
  <h:outputText value="#{cc.childCount}"/> 
</composite:implementation>

When insertChildren is used I get both children rendered and 0 at the end:

<composite:implementation>
  <composite:insertChildren/>
  <h:outputText value="#{cc.childCount}"/> 
</composite:implementation>

Whereas my goal is:

<composite:implementation>
  <composite:insertChildren/>
  <h:panelGroup rendered="#{cc.childCount == 0}">
    some default data
  </h:panelGroup> 
</composite:implementation>

Any ideas/workarounds?

like image 898
andbi Avatar asked Jun 16 '11 19:06

andbi


1 Answers

Put the children in a panelGroup with an id (eg children).

Then

#{component.findComponent('children').childCount}

will give you the correct value. Good luck!

like image 90
Mike Braun Avatar answered Oct 15 '22 07:10

Mike Braun