评估是否设置了MethodExpression属性(获取PropertyNotFoundException) [英] Evaluating if MethodExpression attribute is set (getting PropertyNotFoundException)
问题描述
我有一个具有MethodExpression属性changeListener
的UI组件:
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
属性的情况下,我才想呈现<p:remoteCommand>
.
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屋!