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).