views:

44

answers:

1

I think the title tells it all: I want to add a row to dataTable over AJAX. The datatable has input fields where user can put text. As simple as that, a pretty common case.

With session-scoped bean this no issue, as the same bean is updated over and over again. However, I want to do this in request scope. For each request, I guess I want to create a new bean and to populate it with the values from my form. Then I want my commandButton's action to add a new row, finally render the dataTable over AJAX as usual.

The issue is that I don't know how to make JSF fill the newly-created request-bean with the current data from the dataTable component?

There was a similar question asked and answered. However, that solution seems to reload the contents of the dataTable each time it is refreshed and manually inserts empty elements for the newly-inserted rows like this:

// Preserve list with newly added items.
ror (int i = 0; i < (Integer) count.getValue(); i++) {
    list.add(new Item());
}

To me, it seems that this approach also wipes the possible changes that the user did to rows (new and old)... if he doesn't first save them.

Any pointers?

+1  A: 

You should really consider using the new JSF 2.0 view scope. This lies between the request and session scope in. This scope lives as long as you're interacting (submitting and navigating to) the same view. This is an exact suit for the particular functional requirement.

See also

BalusC
The problem with this is that if the user opens the same page into different browser tabs, all the `@ViewScope` beans are still shared for all the tabs. So every "instance" of the same page still shares the same beans. Or might I have misunderstood this? Looking at your links next...
Tuukka Mustonen
No, certainly not. Each tab is a new view at its own. What you're describing is true for the session scope. Try it out, it's really handy. I am now almost only using view scoped beans all the time. It's an ideal suit.
BalusC
We are running Spring underneath, so I tried `@ViewScope` ported to Spring, using this implementation: http://cagataycivici.wordpress.com/2010/02/17/port-jsf-2-0s-viewscope-to-spring-3-0/. At least this seems to share the beans between different tabs. I wonder if this implementation is a little faulty then, compared to the original `@ViewScope`. Have to debug it. I am starting to wonder whether I should completely convert to native JEE6, modify the scope implementation, or try Seam on top of Spring :/ Do you have a suggestion?
Tuukka Mustonen
Sorry, I don't do Spring/Seam, so I can't objectively answer this.
BalusC
Ok, after trying to debug the Spring-ported ViewScope, I find it working. Wonder if I my changes did not really refresh to code previously (I often use Java debugger code changes without re-deployment). The Spring-implementation seems to function as BalusC described. The beans are stored inside `FacesContext`'s viewMap, so it's different for each view.
Tuukka Mustonen