2012-12-04 16 views
8

我需要驗證是否已在我的複合組件中傳遞了可選屬性。我怎樣才能做到這一點?如何檢查複合組件中是否存在可選屬性

<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屬性xxx<composite:attribute>是不是我要找的。

回答

7

你可以檢查是否#{not empty cc.attrs.attr2}評估爲true

E.g.任意成分的rendered屬性內:

<composite:implementation> 
    <h:panelGroup rendered="#{not empty cc.attrs.attr2}"> 
     Attribute attr2 is not empty! 
    </h:panelGroup> 
</composite:implementation> 
+0

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

我發現答案。 – MyFist

+0

它沒有按照我的預期工作。它正在檢查'attr2'的VALUE值是否爲空(空),並進行相應的渲染。但是如果'attr2'本身存在或者不存在,我想添加一個檢查(在我的CC實現中)。說我對'>的檢查應該給我'true',因爲'attr2'本身已經通過了。 AND' '應該給我'false',因爲'attr2'本身沒有通過。請建議。 – MyFist

+0

你可以比較'null'。 'rendered =「#{cc.attrs.attr2!= null}」'。 – BalusC

3

可以檢查以查看是否存在表達使用方法:

cc.getValueExpression( 'someAttribute')

<composite:implementation> 
    <h:outputText rendered="#{cc.getValueExpression('attr2') ne null}"> 
     Attribute attr2 has been passed! 
    </h:outputText> 
</composite:implementation> 
+0

如果我是他,我會選擇這個作爲接受的答案。 – webyildirim