They needs to be created and added as follows (copied from one of my previous answers):
FacesContext context = FacesContext.getCurrentInstance();
MethodExpression actionListener = context.getApplication().getExpressionFactory()
.createMethodExpression(context.getELContext(), "#{bean.actionListener}", null, new Class[] {ActionEvent.class});
uiCommandComponent.addActionListener(new MethodExpressionActionListener(actionListener));
...where #{bean.actionListener}
actually exists and is declared like follows in the backing bean class associated with the managed bean name bean
:
public void actionListener(ActionEvent event) {
// ...
}
More importantingly, you need to give any dynamically created UICommand
(and UIInput
) component in question a fixed ID as well, else it will get an autogenerated ID which may cause that JSF cannot locate/correlate it during the apply request values phase.
Thus, do so as well:
uiCommandComponent.setId("someFixedId");