13

我需要验证我的复合组件中是否传递了可选属性。我怎样才能做到这一点?

<composite:interface>
    <composite:attribute name="attr1" />
    <composite:attribute name="attr2" required="false" /> <!-- means OPTIONAL -->
</composite:interface>
<composite:implementation>
    <!-- How I can verify here whether attr2 is present or not whenever this component is used? -->
</composite:implementation>

default属性设置为xxxfor<composite:attribute>不是我想要的。

4

3 回答 3

13

您可以检查是否#{not empty cc.attrs.attr2}评估为true.

例如rendered,在任意组件的属性内:

<composite:implementation>
    <h:panelGroup rendered="#{not empty cc.attrs.attr2}">
        Attribute attr2 is not empty!
    </h:panelGroup>
</composite:implementation>
于 2012-12-04T13:32:14.270 回答
3

您可以使用以下方法检查表达式是否存在:

cc.getValueExpression('someAttribute')

<composite:implementation>
    <h:outputText rendered="#{cc.getValueExpression('attr2') ne null}">
        Attribute attr2 has been passed!
    </h:outputText>
</composite:implementation>
于 2016-02-15T02:10:45.070 回答
1

您可以通过以下方式有条件地向组件添加属性:

<c:if><f:attribute>

样本:

<composite:interface>
    <composite:attribute name="label" />
    <composite:attribute name="required" default="false" />
    <composite:attribute name="readonly" default="false" />
    <composite:attribute name="value" />
    <composite:attribute name="title" />
    <composite:attribute name="placeholder" />
    <composite:attribute name="maxlength" type="java.lang.Integer"/>
</composite:interface>
<composite:implementation>
    <p:inputText
      id="field"
      value="#{cc.attrs.value}">
        <c:if test="#{empty cc.attrs.maxLength}">
           <f:attribute name="maxlength" value="#{cc.attrs.maxlength}" />
        </c:if>
    </p:inputText>
</composite:implementation>

我在以下位置找到了答案:

如果组合组件为空,如何不设置组件的属性?

于 2017-10-06T13:33:16.360 回答