How to set HtmlCommandButton action and listener

How can I please set a HtmlCommandButton action and specify the listener which will be executed when the action will be fired .Thanks in advance.
[159 byte] By [WebServicea] at [2007-11-27 5:15:01]
# 1
Are you using JSF 1.1 or JSF 1.2?
BalusCa at 2007-7-12 10:37:10 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
JSF 1.2
WebServicea at 2007-7-12 10:37:10 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

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'.

BalusCa at 2007-7-12 10:37:10 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...