JSF / RichFaces - 如何以编程方式从HtmlAjaxSupport调用ActionEvent(a4j:支持)

时间:2014-01-31 18:17:01

标签: java jsf richfaces

我正在做这个

htmlAjaxSupport.setActionExpression(createMethodExpression(
                        "#{questionarioMB.visualizarQuestoesFilhos}", null,
                        new Class[0]));

方法

public void visualizarQuestoesFilhos()

被召唤。

但我想打电话

 public void visualizarQuestoesFilhos(ActionEvent event)

因为这样,我可以获得点击哪个组件。

htmlAjaxSupport 设置需要什么才能正常工作?

1 个答案:

答案 0 :(得分:1)

在JSF 1.2或更新版本中创建ActionListener表达式:

FacesContext context = FacesContext.getCurrentInstance();
MethodExpression actionListener = context.getApplication().getExpressionFactory()
    .createMethodExpression(context.getELContext(), "#{bean.actionListener}", null, new Class[] {ActionEvent.class});
uiCommandComponent.addActionListener(new MethodExpressionActionListener(actionListener));

为了避免大量的样板代码,你可以很好地将它包装在辅助方法中(如果需要在helper / utility类中),例如:

public static MethodExpression createAction(String actionExpression, Class<?> returnType) {
    FacesContext context = FacesContext.getCurrentInstance();
    return context.getApplication().getExpressionFactory()
        .createMethodExpression(context.getELContext(), actionExpression, returnType, new Class[0]);
}

public static MethodExpressionActionListener createActionListener(String actionListenerExpression) {
    FacesContext context = FacesContext.getCurrentInstance();
    return new MethodExpressionActionListener(context.getApplication().getExpressionFactory()
        .createMethodExpression(context.getELContext(), actionListenerExpression, null, new Class[] {ActionEvent.class}));
}

从Adrian提供的另一个stackovetraread获取片段:

Deprecated richfaces javax.faces.el.MethodBinding replacement use