I am trying to add commandlink programatically, but I am not able to add action.
HtmlCommandLink link = new HtmlCommandLink();
link.setValue(data);
link.setActionExpression(no idea);
How do I create it?
Use ExpressionFactory#createMethodExpression().
Here's a convenience method:
private static MethodExpression createMethodExpression(String expression, Class<?> returnType) {
FacesContext context = FacesContext.getCurrentInstance();
return context.getApplication().getExpressionFactory().createMethodExpression(
context.getELContext(), expression, returnType, new Class[0]);
}
Here's how you could use it, provided that you've a public String doSomething() {} action in a managed bean identified by #{bean}:
link.setActionExpression(createMethodExpression("#{bean.doSomething}", String.class));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With