评估是否设置了 MethodExpression 属性(获取 PropertyNotFoundException) [英] Evaluating if MethodExpression attribute is set (getting PropertyNotFoundException)
问题描述
我有一个带有 MethodExpression 属性的 UI 组件 changeListener
:
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>
这个 changeListener
属性是一个可选的方法表达式,在 remoteCommand
中用作 actionListener,我想渲染 changeListener
属性已设置时.
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}">
和
<p:remoteCommand rendered="#{cc.attrs.changeListener != null}" />
但是我得到一个 javax.el.PropertyNotFoundException 因为它试图将属性作为一个属性来评估.
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 ?
谢谢
推荐答案
<c:if>
你的方向是正确的.rendered
永远不会工作.您只需要检查 EL设置表达式,而不是实际将整个 EL 表达式评估为值表达式并检查其结果是否不为空,如果 EL 表达式表示方法表达式,这当然会失败.
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>
然而,这个解决方案有点吓人:你在这里将方法表达式作为值表达式来获取.但是,只要您不实际评估封闭的 EL 表达式(就像您最初的 #{cc.attrs.changeListener}
尝试在幕后所做的那样),那么就没有任何问题.没有其他干净的方法,因为在 JSF API 中没有像 UIComponent#getMethodExpression()
这样的东西.
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.
这篇关于评估是否设置了 MethodExpression 属性(获取 PropertyNotFoundException)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!