2

I am trying to create a dynamic form with input text and command button. Everything works fine. But when I click on the command button, the action listener is never called. Please suggest what I am doing wrong or if this is a bug with PF or Mojarra. The code is below

panel = new Panel();
panel.setHeader("Test");

InputText text = new InputText();

final String binding = "#{roleCreateForm.role.name}";

text.setValueExpression("value",
           createValueExpression(binding, String.class));

panel.getChildren().add(text);

CommandButton button = new CommandButton();
button.setValue("Save");

MethodExpression me = createMethodExpression("#{roleCreateForm.save}");

button.addActionListener(new MethodExpressionActionListener(me));

panel.getChildren().add(button);

Also the createXXXExpression are below

private MethodExpression createMethodExpression(String action) {
  final Class<?>[] paramTypes = new Class<?>[0];

  MethodExpression methodExpression = getExpressionFactory()
    .createMethodExpression(getELContext(),action, null, paramTypes);

  return methodExpression;
}

private ValueExpression createValueExpression(String binding,
     Class<String> clazz) {
  final ValueExpression ve = getExpressionFactory()
        .createValueExpression(getELContext(), binding, String.class);
  return ve;
}


public static ELContext getELContext() {
  return FacesContext.getCurrentInstance().getELContext();
}

public static ExpressionFactory getExpressionFactory() {
  return getApplication().getExpressionFactory();
}

public static Application getApplication() {
  return FacesContext.getCurrentInstance().getApplication();
}

My form bean is below

public void save() {
  logger.info("Saving role - {}" , role);
}

I am using Primefaces 3.2, Mojarra 2.1.7, Tomcat 7, JDK 6 , Ubuntu 11

Here is my modified code Yes I have seen that you have pointed out this as the common mistake. But here is my modified code. This does not work either.

public Panel getPanel() {
  if (panel == null) {
    panel = new Panel();
    panel.setHeader("Test");
    panel.setId("dynapanel");

    InputText text = new InputText();
    text.setId("dynatext");

    final String binding = "#{roleCreateForm.role.name}";

    text.setValueExpression("value", createValueExpression(binding, String.class));

    panel.getChildren().add(text);

    CommandButton button = new CommandButton();
    button.setValue("Save");

    MethodExpression me = getExpressionFactory().createMethodExpression(getELContext(),    "#{roleCreateForm.save}", void.class, new Class[0]);
    AjaxBehavior ajaxBehavior = new AjaxBehavior();
    //ajaxBehavior.setListener( me );
    ajaxBehavior.addAjaxBehaviorListener( new    AjaxBehaviorListenerImpl( me ) );
    button.addClientBehavior( "submit", ajaxBehavior);


    panel.getChildren().add(button);

  }
  return panel;
}               
Dominic Weiser
  • 1,446
  • 1
  • 20
  • 32
dhrubo
  • 171
  • 1
  • 9

2 Answers2

-1

As far as I remember, if you want to invoke a method in your backing bean, use a MethodExpression as a Listener of your AjaxBehaviour:

        AjaxBehavior ab1 = new AjaxBehavior();
        ExpressionFactory ef = ctx.getApplication().getExpressionFactory();
        MethodExpression me1 = ef.createMethodExpression(ctx.getELContext(),
                                     expression,//Your ELExpression #{roleCreateForm.save}
                                     expectedReturnType, //In your case null
                                     expectedParamTypes); //If you receive parameters put new Class[]{Object.class});
        ab1.setListener(me1);
        button.addClientBehavior( "submit", ab1);
amorales
  • 41
  • 4
  • Unfortunately this does not work either. I have changed the strategy a bit please see next. – dhrubo May 08 '12 at 05:28
  • Did u resolved it ? well, i've a similar problem and i've some more steps ahead in order to resolve it (i found a workaround for this issue in the primefaces forum).Have a look here : http://stackoverflow.com/questions/15808956/how-to-programmatically-add-an-ajaxbehavior-to-a-uicomponent-with-primefaces – Bardelman Apr 04 '13 at 14:20
-1
CommandButton btn = ((CommandButton) FacesContext.getCurrentInstance().getViewRoot().findComponent("full id of button"));

try{
    FacesContext context = FacesContextWrapper.getCurrentInstance();
    MethodExpressionActionListener methodExpression = new MethodExpressionActionListener(context.getApplication().getExpressionFactory()
                .createMethodExpression(context.getELContext(),"#{bean.method}", null, new Class[] {ActionEvent.class}));

    btn.addActionListener(methodExpression);
}catch(Exception exc){
    exc.printStackTrace();
}

and createMethodExpression :

public static MethodExpression createMethodExpression(String expression, Class<?> returnType, Class<?>... parameterTypes) {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    return facesContext.getApplication().getExpressionFactory().createMethodExpression(
        facesContext.getELContext(), expression, returnType, parameterTypes);
}

This works for me ;)

Michal Heneš
  • 353
  • 2
  • 4
  • 19
  • this is not creating a button programmatically (as was the question) but adding functionality to an already available button – Kukeltje Aug 07 '15 at 09:19
  • 1
    "Everything works fine. But when I click on the command button, the action listener is never called. Please suggest what I am doing wrong"... I think that adding listener to new button is the same – Michal Heneš Aug 09 '15 at 17:05