How to evaluate MethodExpressions in JSF components

I'm not sure about the β€œright” way to handle method expressions in composite components.

My team uses a support class with action methods. Abstracts perform some default action or delegate the action method passed as an attribute to the composite user:

When using the page:

<my:component action="#{myBean.actionMethod}" /> 

Composite:

 <cc:interface componentType="mycomponentType"> <cc:attribute name="action" method-signature="java.lang.String action()" required="false" /> </cc:interface> <cc:implementation> <h:commandButton value="submit" action="#{cc.componentAction}" /> </cc:implementation> 

Support Class:

 @FacesComponent("mycomponentType") public class UIMyComponent extends UINamingContainer { public String action() { String outcome = ""; ValueExpression ve = getValueExpression("action"); String expression = ve.getExpressionString(); FacesContext facesContext = FacesContext.getCurrentInstance(); Application application = facesContext.getApplication(); ELContext elContext = facesContext.getELContext(); ExpressionFactory expressionFactory = application .getExpressionFactory(); MethodExpression methodExpression = expressionFactory.createMethodExpression(elContext, expression, String.class, new Class[0]); outcome = (String) methodExpression.invoke(elContext, new Object[0]); if (outcome.equals("whatever")) { // set another outcome } return outcome; } } 

The above code works as expected, but I find it rather cumbersome and creates a ValueExpression to extract the method expression from the declared "action".

UIComponentBase offers getValueExpression("attributeName") , but there is nothing like MethodExpressions.

So my question is, is there a better way to evaluate MethodExpressions declared as attributes in composite components than the code above.

thanks

+4
source share
1 answer

Get it as an attribute instead of expressing a value.

So instead

 ValueExpression ve = getValueExpression("action"); 

make

 MethodExpression me = (MethodExpression) getAttribute("action"); 
+4
source

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


All Articles