views:

54

answers:

1
A: 

You need to ensure that exactly the same datamodel is preserved during the apply request values phase of the form submit as it was during the render response phase of the initial page display. A normal place is to do the preloading is the constructor of the backing bean.

public class Bean {
    private List<Item> items;

    public Bean() {
        items = itemDAO.list();
    }

    // ...
}

An alternative is to put the bean in the session scope, but this has more impact on user experience.

Since you're using Tomahawk, you can also just set the preserveDataModel attribute of the t:dataTable to true like so:

<t:dataTable preserveDataModel="true">

This will store the datamodel in the view's component tree (which in turn is stored in the session scope) so that exactly the same datamodel is available in the subsequent request.

See also:

BalusC
thank for your reply and your useful tutorial but it didn't solve my problem :(
arash
Is the generated HTML valid? I.e. there is no nested `<form>` element?
BalusC
comment's length is not enough so i post a new answer!
arash
Well, then I don't see other possible causes than wrong data loading logic. I.e. the preserved datamodel is not the same. Did you try putting the whole bean in session scope as a test? You only need to ensure that your getters are just returning data and not doing anything else. Data loading ought to be done in constructors or at highest by lazy loading.
BalusC
Since you're using Tomahawk's datatable, you could also try to set its `preserveDataModel` attribute to `true`.
BalusC
sorry again!i am new to JSF. let me talk more about my problem. i want to implement dynamic key-value editor. my editor must be able to edit a list of key-value attributes based on model of collection(list of attribute names, their types and their values) the model is saved on two DB tables one master table for model and one detail table for attributes. i have written my bean (above) to encapsulate my model functionality. my managed bean in faces-config.xml has two parameter that gets from url like #{param.primaryKey} i pass model id and model type to my jsf page and i expect to edit it!
arash
my model show correctly but i could not edit it because JSF binding of inputtext tag not work and does not call the setXXX method of my bean!
arash
The setters won't be called when the data model is loaded at the wrong moment in the JSF lifecycle. See [this article](http://balusc.blogspot.com/2006/09/debug-jsf-lifecycle.html). The data model MUST have been loaded *before* the second phase of the form submit. Did you try `preserveDataModel="true"` anyway?
BalusC
:( i am new to JSF, i dont understand what you said about "wrong moment in the JSF lifecycle" as i siad before when i put <inputtext tag outside the <datatable tag every things works fine but when i put it inside it does not call setter! what should i do?! :( :( :'(
arash
As said, either set `preserveDataModel="true"`, or load datamodel in bean constructor, or put bean in session scope (and do NOT load in a getter, unless lazy loading). All boils down that the same datamodel should be preserved during apply request values phase of the form submit as it was during render response phase of the initial request. I've given enough links. I suggest to put the current project aside and create a brand new playground project based on the code/lessons in the given links and play around until you understand it. Then apply the newly learnt things on your current project.
BalusC
you were right! the problem has been solved by preserveDataModel="true" thanks again
arash
You're welcome. You should then mark the answer accepted.
BalusC