How to check presence of optional attribute inside a composite component
Asked Answered
C

3

13

I need to verify whether an optional attribute has been passed or not within my composite component. How can I achieve this?

<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>

Setting the default attribute to xxx for <composite:attribute> is not what I'm looking for.

Conciliatory answered 4/12, 2012 at 12:17 Comment(0)
G
13

You could just check if #{not empty cc.attrs.attr2} evaluates to true.

E.g. inside the rendered attribute of an arbitrary component:

<composite:implementation>
    <h:panelGroup rendered="#{not empty cc.attrs.attr2}">
        Attribute attr2 is not empty!
    </h:panelGroup>
</composite:implementation>
Galosh answered 4/12, 2012 at 13:32 Comment(5)
by the way it is checking the value of attr2 is empty or not and rendering accordingly.Conciliatory
It is not working as I expected. It is checking the VALUE of attr2 is empty (null) or not and rendering accordingly. But I want to add a check (with in my CC implementation) if attr2 itself is PRESENT or not. Say my check on <cc:myComponent attr1="" attr2=""> should give me true as attr2 itself is passed. AND <cc:myComponent attr1=""> should give me false as attr2 itself is not passed. Please suggest.Conciliatory
You could compare to null. rendered="#{cc.attrs.attr2 != null}".Galosh
What's the functional requirement for this strange approach?Galosh
Sorry for late reply. There is no functional requirement but I want my CC component simple to look.Conciliatory
T
4

You can check to see if the expression exists using the method:

cc.getValueExpression('someAttribute')

<composite:implementation>
    <h:outputText rendered="#{cc.getValueExpression('attr2') ne null}">
        Attribute attr2 has been passed!
    </h:outputText>
</composite:implementation>
Threnode answered 15/2, 2016 at 2:10 Comment(2)
If i were him, i would choose this one as accepted answer.Garay
This is more helpful than the accepted answer, see also https://mcmap.net/q/17658/-jsf-2-composite-component-with-optional-listener-attribute-on-f-ajaxSlug
P
1

You can conditionally add attributes to component via:

<c:if><f:attribute>

Sample:

<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>

I Found the answer in:

How not to set an attribute of a component inside a composite component if it is empty?

Previse answered 6/10, 2017 at 13:33 Comment(1)
This will result in a null pointer exception, you have to check for not empty: ` <c:if test="#{not empty cc.attrs.maxLength}">`Sivia

© 2022 - 2024 — McMap. All rights reserved.