tags:

views:

61

answers:

1

One advantage of the datamodel is that you get the row index in a table. But I find this unnecessary, since you can access the current row element using the var attribute of h:datatable. And I often need to convert to the datamodel to list, since some component libraries are expecting a list. I am thinking of completely abandoning DataModel. What do you think? Are there any advantages of DataModel.

Thanks, Theo

+2  A: 

Another advantage is that you can obtain the currently processed row by DataModel#getRowData(). This is particularly useful when you want to access the current row during events (conversion/validation, value change listener, action method, etc).

E.g.

<h:column>
    <h:commandButton value="edit" action="#{bean.edit}" />
</h:column>

with

public String edit() {
    currentItem = dataModel.getRowData();
    // ...
}

You can find a basic CRUD example which utilizes this in this blog. If you wasn't using DataModel, you would be forced to use f:setPropertyActionListener for this which is only clumsy and won't work for a validator/converter or value change listener. Since JSF 2.2 EL, you could also pass the current var item as method argument like so:

<h:commandButton value="edit" action="#{bean.edit(item)}" />

While nice, this would only make your webapp incompatible with Java EE 5 based containers.

As to the overhead, the "conversion" from List<T> to DataModel<T> is particularly cheap. No new items are been copied or created or so, it's just a wrapper class which delegates the methods to the wrapped class and adds another methods to it (see also the adapter pattern).

BalusC
Can we conclude that the `DataModel` exists for only two functions: for providing row information (not needed with EL 2.2) and for allowing listeners (guess this could be nice sometimes)? A note: using EL 2.2 does not render application incompatible with JEE5 containers (you can manually update their EL libraries), it does render the application incompatible with JEE5 spec, however.
Tuukka Mustonen