Search code examples
javajsfjsf-2lifecycle

Why does the render response phase use old data?


I have a dataTable and in each row there is a commandButton which calls a method from a ManagedBean with the current item id like this:

<h:commandButton action="#{myBean.doStuff(item.id)}" value="click me"/>

This works pretty good, except the render response phase calls the doStuff method again with another (probably old?) id. I looked into this issue with a PhaseListener:

...stuff...
INFO: START PHASE INVOKE_APPLICATION 5
INFO: selecting item with Id: 11
INFO: selected item: new_item
INFO: END PHASE INVOKE_APPLICATION 5
INFO: START PHASE RENDER_RESPONSE 6
INFO: selecting item with Id: 3
INFO: selected item: test1
INFO: END PHASE RENDER_RESPONSE 6
...more stuff...

new_item is what i want and test1 is what i get.

So why does the doStuff method get called in the render response phase and why does it use wrong values?

edit: I am using JSF 2.0


Solution

  • This normally wouldn't be a problem. E.g. consider the following facelet:

    <html xmlns="http://www.w3.org/1999/xhtml"
        xmlns:h="http://java.sun.com/jsf/html"
        xmlns:f="http://java.sun.com/jsf/core">
    
        <h:body>    
            <h:form>
    
                <h:dataTable value="#{tableBean.items}" var="item">
    
                    <h:column>
                        #{item}                
                    </h:column>
    
                    <h:column>
                        <h:commandButton value="click" action="#{tableBean.doAction(item)}" />
                    </h:column>
    
                </h:dataTable>
    
            </h:form>
    
        </h:body>
    </html>
    

    and the following backing bean:

    @ManagedBean
    @ViewScoped
    public class TableBean {
    
        private List<String> items;
    
        @PostConstruct
        public void init() {        
            items = new ArrayList<String>();
            items.add("bar");
            items.add("kaz");
            items.add("foo");        
        }
    
        public String doAction(String string) {
            System.out.println(string);
            return "";
        }
    
        public List<String> getItems() {
            return items;
        }    
    
    }
    

    This doesn't print anything extra in the render response phase. Perhaps you need to show more code. You could additionally try to abstract your problem to the bare essentials. If it then works step by step add your other code again until you see it breaking.