Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSF 2.0: Is there any advantage in using DataModel<T> over List<T> for CRUD applications

Tags:

java

jsf

jsf-2

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

like image 975
Theo Avatar asked Oct 20 '10 08:10

Theo


1 Answers

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() {
    Item item = 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 EL 2.2, you could also pass the current var item as method argument like so:

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

with

public String edit(Item 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).

like image 177
BalusC Avatar answered Oct 29 '22 06:10

BalusC