Evaluating if MethodExpression attribute is set (getting PropertyNotFoundException)

假装没事ソ 提交于 2019-12-19 07:23:20

问题


I have a UI component with a MethodExpression attribute 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>

This changeListener attribute is an optional method expression used as actionListener in the remoteCommand and I want to render the <p:remoteCommand> ONLY IF the changeListener attribute has been set.

I have tried several ways to check whether the attribute is set or not, especially:

<c:if test="#{! empty cc.attrs.changeListener}">

and

<p:remoteCommand rendered="#{cc.attrs.changeListener != null}" />

But I get a javax.el.PropertyNotFoundException because it tries to evaluate the attribute as a property instead.

How can I evaluate whether the optional method attribute is set or not ?

thanks


回答1:


You was in the right direction with <c:if> already. The rendered one is never going to work. You only need to check if the EL expression is been set instead of actually evaluating the whole EL expression as a value expression and checking if its result is not empty, which would of course fail if the EL expression represents a method expression.

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

This solution is however somewhat scary: you're grabbing the method expression as a value expression here. However, as long as you don't actually evaluate the enclosed EL expression (like as what your initial #{cc.attrs.changeListener} attempt does under the covers), then there's nothing at matter. There's no other clean way as there's nothing like UIComponent#getMethodExpression() in JSF API.



来源:https://stackoverflow.com/questions/18964325/evaluating-if-methodexpression-attribute-is-set-getting-propertynotfoundexcepti

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!