I have simplify my problem as much as possible. Basically, a Map
is not been printed correctly after I make some modifications on it.
I have the following xhtml code:
<c:forEach items="#{personController.groupedPersons}" var="personG">
<p>
<h:commandButton action="#{personController.removePersonGroup(personG.key)}" type="submit" value="Remove">
<f:ajax render="@form" />
</h:commandButton>
</p>
<p>#{personG.key}</p>
<p>#{personG.value.size()}</p>
</c:forEach>
This iterates over a Map
and prints its information. For each key, there is a button that allows removing it from the Map.
With this code in my personController
bean (which is @ViewScoped
and implements Serializable
):
private HashMap<Integer, List<Person>> groupedPersons;
public void removePersonGroup(Integer age) {
groupedPersons.remove(age);
}
My testing data has key=20 and 3 values (Person) and key=30 and 1 value (Person). The first time I load the page and print the map it works fine.
- 1st test: I press the button for the 2nd group (key 30). It removes the group and prints the Map just fine, with the remaining group. Then press the button for the 1st group (key 20). It removes the group and prints the Map just fine, which is empty.
- 2nd test: I reload the page. I press the button for the 1st group (key 20). My traces shows that
removePersonGroup()
works fine andgroupedPersons
has only the single element of key=30. However, when ajax refreshes the form, the XHTML shows#{personG.key} = 20
(which has been removed) and#{personG.value.size()} = null
(nothing is shown, thevalue
is empty). What am I doing wrong? I cannot find the problem
UPDATE
After doing the 2nd test and having the wrong deta displayed in the XHTML, when I refresh the form with another button and refresh the whole page, now the XHTML shows the actual data correctly! So there must be something (concurrency related?) that is making the XHTML show the wrong data.