【问题标题】:Evaluating if MethodExpression attribute is set (getting PropertyNotFoundException)评估是否设置了 MethodExpression 属性(获取 PropertyNotFoundException)
【发布时间】:2013-09-23 16:29:09
【问题描述】:

我有一个带有 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>

这个changeListener 属性是一个可选的方法表达式,用作remoteCommand 中的actionListener,我只想在changeListener 属性已设置的情况下呈现&lt;p:remoteCommand&gt;

我尝试了几种方法来检查属性是否设置,特别是:

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

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

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

如何判断是否设置了可选方法属性?

谢谢

【问题讨论】:

    标签: jsf jsf-2 conditional composite-component methodexpression


    【解决方案1】:

    &lt;c:if&gt; 的方向是正确的。 rendered 永远不会工作。你只需要检查EL expression是否被设置,而不是实际将整个EL表达式作为值表达式求值并检查其结果是否不为空,如果EL表达式表示方法表达式,这当然会失败。

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

    然而,这个解决方案有点吓人:你在这里将方法表达式作为值表达式来获取。但是,只要您不实际评估封闭的 EL 表达式(就像您最初的 #{cc.attrs.changeListener} 尝试在幕后所做的那样),那么就没有任何问题。没有其他干净的方法,因为在 JSF API 中没有像 UIComponent#getMethodExpression() 这样的东西。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-09
      • 2019-04-15
      • 2011-08-22
      相关资源
      最近更新 更多