grubi
grubi

Reputation: 165

StateHelper strange behavior difference for put and add

I'm currently implementing a backing component for a composite component. I'm using the state helper to keep the internal state during requests and I found a strange behavior I can't explain to myself.

The composite component has a button which adds a new item to an ArrayList. This list was created by myself and added to the state. This is the code:

public void buttonActionListener() {
    List<Item> itemList = getItemList();
    if(itemList == null) {
        itemList = new ArrayList<>();
        setItemList(itemList);
    }
    itemList.add(item);
}

public List<Item> getItemList() {
    return (List<Item>) getStateHelper().get(PropertyKeys.itemList);
}

private void setItemList(List<Item> itemLis) {
    getStateHelper().put(PropertyKeys.itemList, itemList);
}

The list is the displayed in the composite component with a dataTable. After the first request/button click I have the list with one item in it. The second click will show me two items in the datatable, but it seems, that nothing is stored to the state helper. Because the third click only displays the items #1 and #3 but #2 is lost. Each succeeding click will always only show me item #1 and #n.

But when I use

public void buttonActionListener() {
    addItemList(item);
}

public List<Item> getItemList() {
    return (List<Item>) getStateHelper().get(PropertyKeys.itemList);
}

private void addItemList(Item item) {
    getStateHelper().add(PropertyKeys.itemList, item); // add instead of put
}

everything works as desired. Both methods (implementations) in the StateHelper do nearly the same. Please could you explain me what's going on?

I had a similar behavior in the past while I was testing around. But at this time I didn't use a list but just the Item which should be changed in the state saving. I always got the first added state, never the changed one.

I'm using Mojarra 2.1.28 on a JBoss 7.1.3.

Upvotes: 1

Views: 358

Answers (0)

Related Questions