You can use the [url=http://java.sun.com/javaee/5/docs/api/javax/el/ExpressionFactory.html]ExpressionFactory[/url], which is available by the FacesContext, to create MethodExpressions and ValueExpressions for the given EL context.// Prepare
FacesContext facesContext = FacesContext.getCurrentInstance();
ExpressionFactory expressionFactory = facesContext.getApplication().getExpressionFactory();
ELContext elContext = facesContext.getELContext();
// Create action button.
HtmlCommandButton actionButton = new HtmlCommandButton();
MethodExpression actionExpression =
expressionFactory.createMethodExpression(elContext, "#{myBean.action}", String.class, new Class[] {});
actionButton.setActionExpression(actionExpression);
actionButton.setValue("invoke action");
// Create actionListener button.
HtmlCommandButton actionListenerButton = new HtmlCommandButton();
MethodExpression actionListenerExpression =
expressionFactory.createMethodExpression(elContext, "#{myBean.actionListener}", null, new Class[] {ActionEvent.class});
actionListenerButton.addActionListener(new MethodExpressionActionListener(actionListenerExpression));
actionListenerButton.setValue("invoke actionListener");
which invokes respectively
public String action() {
// Implement.
}
public void actionListener(ActionEvent event) {
// Implement.
}
If you want to declare action() void, then replace 'String.class' by 'null'.