JSF2复合组件:是#{cc.childCount}和<;组合:insertChildren/>;相互排斥?
我就是不明白: 如果我想让我的复合组件插入子组件,我使用JSF2复合组件:是#{cc.childCount}和<;组合:insertChildren/>;相互排斥?,jsf,jsf-2,composite-component,Jsf,Jsf 2,Composite Component,我就是不明白: 如果我想让我的复合组件插入子组件,我使用,但是{cc.childCount}在这种情况下总是返回0。另一方面,如果我不使用我总是得到正确的儿童计数,而不会渲染儿童。为什么会这样 在我的组件中,我只想在没有子级的情况下渲染一些“默认”面板,而在其他情况下不渲染它-行为类似于默认值。因此,我需要insertChildren和childCount这两个似乎不在一起工作 代码如下: <my:test> <h:outputText value="child1" ren
,但是{cc.childCount}
在这种情况下总是返回0
。另一方面,如果我不使用
我总是得到正确的儿童计数
,而不会渲染儿童。为什么会这样
在我的组件中,我只想在没有子级的情况下渲染一些“默认”面板,而在其他情况下不渲染它-行为类似于默认值
。因此,我需要insertChildren和childCount这两个似乎不在一起工作
代码如下:
<my:test>
<h:outputText value="child1" rendered="#{some.trueValue}"/>
<h:outputText value="child2" rendered="#{some.trueValue}"/>
<my:test>
当使用insertChildren
时,我会同时渲染子级和0
:
<composite:implementation>
<composite:insertChildren/>
<h:outputText value="#{cc.childCount}"/>
</composite:implementation>
鉴于我的目标是:
<composite:implementation>
<composite:insertChildren/>
<h:panelGroup rendered="#{cc.childCount == 0}">
some default data
</h:panelGroup>
</composite:implementation>
一些默认数据
有什么想法/解决方法吗?将孩子们放在一个有id的小组中(如children) 然后
将为您提供正确的值。祝你好运 我也有类似的问题。我切换到
c:if
并且它起作用了,所以在你的情况下它会起作用
<composite:implementation>
<composite:insertChildren/>
<c:if test="#{cc.childCount == 0}">
some default data
</c:if>
</composite:implementation>
一些默认数据
来自cc:insertChildren文档:
使用页面中复合组件标记中的任何子组件或模板文本将在该标记在节中的位置所指示的点处重新设置为复合组件的父级
因此,通过使用
您实际上将子项从{cc}
组件移动到您指定的
组件中,有效地使它们成为{cc}
的(曾孙)。为了方便访问这些重新定位的子对象,我使用
作为父对象:
<ui:fragment binding="#{childContainer}">
<cc:insertChildren/>
</ui:fragment>
现在您可以将该类绑定到复合组件:
<?xml version='1.0' encoding='UTF-8' ?>
<ui:component
xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:cc="http://java.sun.com/jsf/composite"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
>
<cc:interface componentType="myCompositeComponent">
</cc:interface>
<cc:implementation>
I have #{cc.childContainer.childCount} children.
<ui:fragment binding="#{cc.childContainer}">
<cc:insertChildren/>
</ui:fragment>
</cc:implementation>
</ui:component>
我有#{cc.childContainer.childCount}个孩子。
现在您有了一个具有
的复合组件,可以直接访问这些子组件
编辑:合并了BalusC注释。getFamily()
是不必要的,因为它已经在UINamingContainer
中实现了。只有在实现NamingContainer
接口或另一个UIComponent
子类时,才需要指定自己。
<ui:fragment binding="#{childContainer}">
<cc:insertChildren/>
</ui:fragment>
package com.stackoverflow.jsfinsertchildrenandcountexample;
import javax.faces.component.FacesComponent;
import javax.faces.component.UIComponent;
import javax.faces.component.UINamingContainer;
@FacesComponent(value = "myCompositeComponent")
public class MyCompositeComponent extends UINamingContainer {
private UIComponent childContainer;
public UIComponent getChildContainer() {
return childContainer;
}
public void setChildContainer(UIComponent childContainer) {
this.childContainer = childContainer;
}
}
<?xml version='1.0' encoding='UTF-8' ?>
<ui:component
xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:cc="http://java.sun.com/jsf/composite"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
>
<cc:interface componentType="myCompositeComponent">
</cc:interface>
<cc:implementation>
I have #{cc.childContainer.childCount} children.
<ui:fragment binding="#{cc.childContainer}">
<cc:insertChildren/>
</ui:fragment>
</cc:implementation>
</ui:component>