评估是否设置了MethodExpression属性(获取PropertyNotFoundException)

Sam*_*uel 6 jsf conditional composite-component jsf-2 methodexpression

我有一个带有MethodExpression属性的UI组件changeListener:

<composite:interface>
  <composite:attribute name="changeListener" required="false" method-signature="void actionListener(javax.faces.event.ActionEvent)" />
  ..
</composite:interface>
<composite:implementation>

  <p:remoteCommand name="ajaxOnChange"
                             update="#{cc.attrs.onChangeUpdate}"
                             oncomplete="#{cc.attrs.onchange}"
                             actionListener="#{cc.attrs.changeListener}" />
  ..
</composite:implementation>
Run Code Online (Sandbox Code Playgroud)

changeListener属性是一个可选的方法表达式,在其中用作actionListenerremoteCommand,我想在<p:remoteCommand>changeListener设置属性的情况下呈现该属性.

我已经尝试了几种方法来检查属性是否设置,尤其是:

<c:if test="#{! empty cc.attrs.changeListener}">
Run Code Online (Sandbox Code Playgroud)

<p:remoteCommand rendered="#{cc.attrs.changeListener != null}" />
Run Code Online (Sandbox Code Playgroud)

但我得到一个javax.el.PropertyNotFoundException,因为它试图将属性评估为属性.

如何评估是否设置了可选方法属性?

谢谢

Bal*_*usC 9

<c:if>已经朝着正确的方向前进了.在rendered一个永远不会去上班.您只需要检查是否已设置EL表达式,而不是实际将整个EL表达式作为值表达式进行计算并检查其结果是否为空,如果EL表达式表示方法表达式,这当然会失败.

<c:if test="#{not empty cc.getValueExpression('changeListener')}">
     ...
</c:if>
Run Code Online (Sandbox Code Playgroud)

然而,这个解决方案有些可怕:你在这里将方法表达式作为值表达式.但是,只要你没有真正评估封闭的EL表达式(就像你最初的#{cc.attrs.changeListener}尝试在封面下做的那样),那么事情就没有了.没有其他干净的方式,因为UIComponent#getMethodExpression()JSF API中没有任何东西.