title: Setting Action/Actionlistener programatically never invoked on JSF 2.2
body: <p>I have tried several ways to implement and adding programatically a commandButon with action or actionListener. I tried to implement an ajax behavior, set an action and set an actionListener without success. In my pom I added <em>el-api</em> dependence because my project didn't got MethodExpression,MethodExpressionActionListener and ExpressionFactory classes and they were necessary in some example codes. I don't get any error messages on glassfish log neither js log.</p>
  
  <p><strong>Edit</strong>
  If I change bean scope to Session the action method is invoked but never with request or view on this <a href="https://stackoverflow.com/questions/12098611/dynamic-jsf-2-0-commandbuttons-set-action-with-arguments">balus example</a></p>
  
  <p><strong>Methods' BeanUtils class</strong></p>
  
  <pre><code>    public static MethodExpression createMethodExpression(String valueExpression,Class&lt;?&gt; expectedReturnType,Class&lt;?&gt;[] expectedParamTypes) {
  
      MethodExpression methodExpression = null;
  
      try {
          FacesContext fc = FacesContext.getCurrentInstance();
          ExpressionFactory factory = fc.getApplication().getExpressionFactory();
  
          methodExpression = factory.createMethodExpression(fc.getELContext(), valueExpression, expectedReturnType, expectedParamTypes);
      } catch (Exception e) {
          throw new FacesException("Method expression '" + valueExpression + "' could not be created.");
      }
  
      return methodExpression;
  }
  
  public static MethodExpressionActionListener createMethodActionListener(String valueExpression,Class&lt;?&gt; expectedReturnType,Class&lt;?&gt;[] expectedParamTypes) {
  
      MethodExpressionActionListener actionListener = null;
  
      try {
          actionListener = new MethodExpressionActionListener(createMethodExpression(
          valueExpression, expectedReturnType, expectedParamTypes));
      } catch (Exception e) {
          throw new FacesException("Method expression for ActionListener '" + valueExpression
          + "' could not be created.");
      }
  
      return actionListener;
  }
  </code></pre>
  
  <p><strong><em>Button code</em></strong> (same for every try)</p>
  
  <pre><code>            CommandButton cancelBtn=(CommandButton)app.createComponent(CommandButton.COMPONENT_TYPE);
              cancelBtn.setId("deleteText_btn_" + panel.getId());
              cancelBtn.setAlt("Change");
              cancelBtn.setTitle("Change");
              cancelBtn.setValue("Change");
  </code></pre>
  
  <p><strong>First try - Ajax behavior</strong></p>
  
  <pre><code>                MethodExpression me = BeanUtils.createMethodExpression("#{eventProvider.deleteText()}",null,new Class&lt;?&gt;[]{BehaviorEvent.class});
              AjaxBehavior ajaxBehavior = (AjaxBehavior) app.createBehavior(AjaxBehavior.BEHAVIOR_ID);
              ajaxBehavior.addAjaxBehaviorListener(new AjaxBehaviorListenerImpl(me, me));
  
              cancelBtn.addClientBehavior("submit",ajaxBehavior);
  </code></pre>
  
  <p><strong>Second try - setting an actionListener</strong></p>
  
  <pre><code>                FacesContext context = FacesContextWrapper.getCurrentInstance();
              MethodExpressionActionListener methodExpression = new MethodExpressionActionListener(context.getApplication().getExpressionFactory()
                      .createMethodExpression(context.getELContext(),"#{eventProvider.deleteText}", null, new Class[] {ActionEvent.class}));
              cancelBtn.addActionListener(methodExpression);
  </code></pre>
  
  <p><strong>Third try - Setting an action</strong></p>
  
  <pre><code>            FacesContext context = FacesContext.getCurrentInstance();
              MethodExpression methodExpression = context.getApplication().getExpressionFactory().createMethodExpression(
                  context.getELContext(), "#{eventProvider.deleteText}", null, new Class[] { ActionEvent.class });
              cancelBtn.setActionExpression(methodExpression);
  </code></pre>
  
  <p><strong>Method which must be called from action/actionListener</strong></p>
  
  <pre><code>   @Component("eventProvider")
     @Scope("view")
     public class EventProvider {
      ............
      ............
      public void deleteText(ActionEvent event){
      logger.entry("EventProvider.deleteText()");
  
       System.out.print("debbuging");
      }
  }
  </code></pre>