title: JSF 2: Create a custom component with CommandButton and get attribute to ActionListener
body: <p>I am new to Stack Overflow and have a question about JSF 2.0 and custom components (i use primefaces 3 too, buts not that important i think). Here is the situation: I have a nested data object  of type <code>AdvancedCriterion</code>, which contains a list of <code>AdvancedCriterion</code> or <code>Criterion</code> (via an interface <code>criteria</code>). These criterions are used to create a complex filter object like this:</p>
  
  <ul>
  <li>Advanced Criterion: AND
  <ul>
  <li>Criterion: PRODUCT equals "ABC"</li>
  <li>Criterion: USER startswith "A"</li>
  <li>AdvancedCriterion: OR
  <ul>
  <li>CRITERION: param1 > 5</li>
  <li>CRITERION: param2 &lt;= 20</li>
  </ul></li>
  </ul></li>
  </ul>
  
  <p>I created two new components in Java: <code>AdvancedCriterion.java</code> and <code>SimpleCriterion.java</code> to do the recursion, because the first attempt to do it with a composite fails.  and recursive call of a composite in a composite creates a stack overflow :-(</p>
  
  <p>Until know i can display a static filter object and it looks fine, but the user should add or delete criterions. So i addes some buttons (here came primefaces into it). Here some code, i started with the first criterion, which is always <code>AdvancedCriterion</code>.</p>
  
  <pre><code>&lt;myTag:advancedCriteriaComponent criteria="#{manageFiltersBean.filterBuilder.criteria}" /&gt;
  </code></pre>
  
  <p>I create know the Button and want to give the parent object to the ActionListener:</p>
  
  <pre><code>CommandButton addButton = new CommandButton();
  addButton.setId("btnAdd" + UUID.randomUUID());
  addButton.setAjax(true);
  addButton.setValue(" + ");
  addButton.addActionListener(new CriteriaActionListener());
  addButton.getAttributes().put("criteria", this.currentCriteria);
  </code></pre>
  
  <p>Well, and here comes the CriteriaActionListener:</p>
  
  <pre><code>@Override
      public void processAction(ActionEvent event) throws AbortProcessingException {
          AdvancedCriteria criteria = (AdvancedCriteria) event.getComponent().getAttributes().get("criteria");
          criteria.addCriteria(new Criterion());
          System.out.println("number of children: " + criteria.getChildren().size());
  
      }
  </code></pre>
  
  <p>In the component the currentCriteria is well known and i can see his child-elements. In the <code>CriteriaActionListener</code> the object is empty (It has the right type, but looks like a fresh initalized object of this type) The question now: How could i get the <code>currentCriteria</code> Object into the <code>CriteriaActionListener</code>?</p>
  
  <p>I tried a attribute in the ActionListener and set it from the component, then the whole object is NULL. I although tried to make an ELExpression and get it right to the bean (#{manageFiltersBean.addCriterion(criteria}) but the object is NULL. I have no more ideas and unfortunatly i am very new to JSF (about a few weeks).</p>
  
  <p>Maybe it is about the id of the buttons? They are random, because the number of buttons is dynamic. I read, that every button must have an unique id to work properly. Or it is something about the attributes/params?</p>
  
  <p>Thank you for your help,</p>
  
  <p>Felix</p>