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 AdvancedCriterion, which contains a list of AdvancedCriterion or Criterion (via an interface criteria). These criterions are used to create a complex filter object like this:
- Advanced Criterion: AND
- Criterion: PRODUCT equals “ABC”
- Criterion: USER startswith “A”
- AdvancedCriterion: OR
- CRITERION: param1 > 5
- CRITERION: param2 <= 20
I created two new components in Java: AdvancedCriterion.java and SimpleCriterion.java 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 🙁
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 AdvancedCriterion.
<myTag:advancedCriteriaComponent criteria="#{manageFiltersBean.filterBuilder.criteria}" />
I create know the Button and want to give the parent object to the ActionListener:
CommandButton addButton = new CommandButton();
addButton.setId("btnAdd" + UUID.randomUUID());
addButton.setAjax(true);
addButton.setValue(" + ");
addButton.addActionListener(new CriteriaActionListener());
addButton.getAttributes().put("criteria", this.currentCriteria);
Well, and here comes the CriteriaActionListener:
@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());
}
In the component the currentCriteria is well known and i can see his child-elements. In the CriteriaActionListener 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 currentCriteria Object into the CriteriaActionListener?
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).
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?
Thank you for your help,
Felix
This problem is solved, some others occur. Well, like commented above, the problem was more like an problem with the implementation of the ‘AdvancedCriteria’ object. There was no connection or hint to the ‘CommandButton’, just a miss-named getter.
Probably the added line
implements Serialiablehelps although, I don’t know exactly, because I tried a lot of things (save and restore states, enumeration propKeys with setters and getters and stuff like this), overall these weren’t the solution.