如何在JSF复合组件中评估MethodExpressions [英] How to evaluate MethodExpressions in JSF composite components

查看:121
本文介绍了如何在JSF复合组件中评估MethodExpressions的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定在复合组件中处理方法表达式的正确"方法.

I am not sure about the "correct" way to deal with method expressions in composite components.

我的复合材料使用带有操作方法的后备类.这些命令执行一些默认操作或委托由复合用户作为属性传递的操作方法:

My composite uses a backing class with action methods. Theses perform some default actions or delegate to an action method passed by the composite user as an attribute:

在使用页面:

<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"属性中检索method-expression.

The code above is working as expected, but I find it rather bulky and it creates a ValueExpression to retrieve the method-expression from the declared "action" attribute.

UIComponentBase提供了getValueExpression("attributeName"),但是MethodExpressions没有类似之处.

UIComponentBase offers getValueExpression("attributeName") but there is nothing similar for MethodExpressions.

所以我的问题是,与上面的代码相比,是否有更好的方法可以评估在复合组件中声明为属性的MethodExpressions.

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

Thx

推荐答案

将其作为属性而不是值表达式获取.

Get it as attribute instead of as value expression.

所以,而不是

ValueExpression ve = getValueExpression("action");

MethodExpression me = (MethodExpression) getAttribute("action");

这篇关于如何在JSF复合组件中评估MethodExpressions的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆