Evaluating MethodExpression attribute (getting PropertyNotFoundException)

I have a user interface component with the MethodExpression changeListener attribute:

 <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 an actionListener in remoteCommand , and I want to display <p:remoteCommand> ONLY IF the changeListener attribute changeListener set.

I tried several ways to check if 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 javax.el.PropertyNotFoundException because it is trying to evaluate the attribute as a property.

How can I determine if an optional attribute of a method is set?

thanks

+6
source share
1 answer

You were already in the right direction with <c:if> . rendered will never work. You only need to check whether the EL> expression has been selected instead of actually evaluating the entire EL expression as a value expression and to check if its result is not empty, which of course, if the EL expression is a method expression.

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

This solution, however, is somewhat intimidating: you capture the method expression as an expression of value here. However, until you actually evaluate the attached EL expression (for example, as your original attempt #{cc.attrs.changeListener} does under covers), then nothing happens in matter. There is no other clean way, as there is nothing like UIComponent#getMethodExpression() in the JSF API.

+7
source

Source: https://habr.com/ru/post/954436/


All Articles