title: Programatically create command button in Primefaces
body: <p>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</p>
  
  <pre><code>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);
  </code></pre>
  
  <p>Also the createXXXExpression are below</p>
  
  <pre><code>private MethodExpression createMethodExpression(String action) {
    final Class&lt;?&gt;[] paramTypes = new Class&lt;?&gt;[0];
  
    MethodExpression methodExpression = getExpressionFactory()
      .createMethodExpression(getELContext(),action, null, paramTypes);
  
    return methodExpression;
  }
  
  private ValueExpression createValueExpression(String binding,
       Class&lt;String&gt; 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();
  }
  </code></pre>
  
  <p>My form bean is below</p>
  
  <pre><code>public void save() {
    logger.info("Saving role - {}" , role);
  }
  </code></pre>
  
  <p>I am using
  Primefaces 3.2, Mojarra 2.1.7, Tomcat 7, JDK 6 , Ubuntu 11</p>
  
  <p><strong>Here is my modified code</strong>
      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.     </p>
  
  <pre><code>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;
  }               
  </code></pre>