title: JSF: How to attach an actionListener to component created programatically?
body: <p>I have to create some commandLinks dynamically and attach some action listener to it, So I've put <code>&lt;h:panelGrid&gt;</code> on the JSP page and used such code to add the commandLinks and to assign action listeners to:</p>
  
  <pre><code>public ManagedBean(){
   List&lt;UIComponenet&gt; child = panelGrid.getChilderen();
   list.clear();
  
   List&lt;MyClass&gt; myList = getSomeList();
  
   for (MyClass myObj : myList){
     FacesContext ctx = FacesContext.getCurrentContext();
     HtmlCommandLink cmdLink = (HtmlCommandLink) ctx.getApplication.createComponent(HtmlCommandLink.COMPONENT_TYPE);
     cmdLink.setValue(myObj.getName());
     cmdLink.setActionLinstner(new ActionListener(){
       public void processAction(ActionEvent event) throws AbortProcessingException{
         System.out.println ("&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;I am HERE ");
       }
     });
     child.add(cmdLink);
   }
  }
  </code></pre>
  
  <p>But unfortunately, when I press this commandLinks, an exception thrown! How can I add component event listeners at runtime?</p>
  
  <p>(Note, the code above my contain syntax/compilation errors as I just wrote).</p>