【问题标题】:How to make composite component similar to <h:selectOneRadio />如何制作类似于 <h:selectOneRadio /> 的复合组件
【发布时间】:2012-09-12 14:58:09
【问题描述】:

我一直在寻找一种方法来制作类似于:&lt;h:selectOneRadio /&gt; 的复合组件 但我没有成功。

我想要类似的东西:

<myowntags:selectOneRadio>
  <f:selectItem itemValue="value0" itemLabel="This is the value 0" />
  <f:selectItem itemValue="value1" itemLabel="This is the value 1" />
  <f:selectItem itemValue="value2" itemLabel="This is the value 2" />
</myowntags:selectOneRadio>

和:

<myowntags:selectOneRadio>
  <f:selectItems  value="#{controller.items}"  />
</myowntags:selectOneRadio>

如你所见,我希望这个复合组件有一个子组件:&lt;f:selectItem /&gt; 并按照我想要的方式渲染它。

提前致谢。

【问题讨论】:

    标签: jsf-2 composite-component


    【解决方案1】:

    您可以通过#{cc.children} 检查和迭代它们。 #{cc} 指的是当前的复合 UIComponent 实例,该实例又具有 getChildren() 方法。您可以通过在&lt;cc:implementation&gt; 中检查孩子的 FQN(或简单名称,如果足够的话)来进行 instanceof 检查:

    <c:forEach items="#{cc.children}" var="child">
        <c:set var="type" value="#{child['class'].simpleName}" />
        <c:if test="#{type == 'UISelectItem'}">
            <input type="radio" value="#{child.itemValue}" />#{child.itemLabel}<br/>
        </c:if>
        <c:if test="#{type == 'UISelectItems'}">
            <c:forEach items="#{child.value}" var="item">
                <input type="radio" value="#{item.value}" />#{item.label}<br/>
            </c:forEach>
        </c:if>
    </c:forEach>
    

    然而,您的下一个问题是收集提交的值。为此,您需要在&lt;cc:interface componentType&gt; 引用的支持UIComponent 中实现decode() 方法。或者,更好的是,使用Renderer 创建自定义UIComponent。在视图中接管Renderer 的工作很笨拙。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-10
      • 2019-05-16
      • 1970-01-01
      • 2016-04-18
      • 1970-01-01
      • 2018-03-23
      • 1970-01-01
      相关资源
      最近更新 更多