JSF2复合组件:#{cc.childCount}和<composite:insertChildren />互斥?

Osw*_*Osw 7 jsf composite-component jsf-2

我只是不明白:如果我希望我的复合组件插入子项,我会使用 <composite:insertChildren/>但在这种情况下#{cc.childCount}总是返回0.另一方面,如果我不使用 <composite:insertChildren/>我总是在childCount没有孩子被渲染的情况下得到正确的.为什么会这样?

我想在我的组件中做的就是在没有子节点时渲染一些"默认"面板,在其他情况下不渲染它 - 行为类似于<ui:insert name="param_name">default value</ui:insert>.所以我需要insertChildren和childCount,它们似乎不能一起工作.

这是代码:

<my:test>
  <h:outputText value="child1" rendered="#{some.trueValue}"/>
  <h:outputText value="child2" rendered="#{some.trueValue}"/>
<my:test>
Run Code Online (Sandbox Code Playgroud)

如果我使用下面的实现,我会2按预期呈现

<composite:implementation>
  <h:outputText value="#{cc.childCount}"/> 
</composite:implementation>
Run Code Online (Sandbox Code Playgroud)

什么时候insertChildren使用我得到两个孩子渲染,0最后:

<composite:implementation>
  <composite:insertChildren/>
  <h:outputText value="#{cc.childCount}"/> 
</composite:implementation>
Run Code Online (Sandbox Code Playgroud)

而我的目标是:

<composite:implementation>
  <composite:insertChildren/>
  <h:panelGroup rendered="#{cc.childCount == 0}">
    some default data
  </h:panelGroup> 
</composite:implementation>
Run Code Online (Sandbox Code Playgroud)

任何想法/解决方法?

Mik*_*aun 5

将孩子放在带有id的panelGroup中(例如孩子).

然后

#{component.findComponent('children').childCount}
Run Code Online (Sandbox Code Playgroud)

会给你正确的价值.祝好运!