我不确定在复合组件中处理方法表达式的"正确"方法。
我的复合使用了一个带有操作方法的后台类。这些执行一些默认操作或委托给复合用户作为属性传递的操作方法:
使用page:
<my:component action="#{myBean.actionMethod}" />
复合:<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>
支持类:
@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;
}
}
上面的代码像预期的那样工作,但是我发现它相当庞大,它创建了一个ValueExpression来从声明的"action"属性中检索方法表达式。
UIComponentBase提供了getValueExpression("attributeName")
,但没有类似的methodexpression。
所以我的问题是,是否有比上面的代码更好的方法来评估在组合组件中声明为属性的MethodExpressions
。
Thx
将其作为属性而不是值表达式。
所以,不用
ValueExpression ve = getValueExpression("action");
MethodExpression me = (MethodExpression) getAttribute("action");