I have a Facelets page with a <h:dataTable>. In each row there is a <h:selectBooleanCheckbox>. If the checkbox is selected the object behind the corresponding row should be set in the bean.
- How do I do this?
- How to get the selected rows or their data in a backing bean?
- Or would it be better to do it with
<h:selectManyCheckbox>?
Your best bet is to bind the
<h:selectBooleanCheckbox>value with aMap<Item, Boolean>property whereItemrepresents the object behind the corresponding row.You see, the map is automatically filled with all table items as key and the checkbox value is automatically set as map value associated with the item as key.
This only requires that the
Item#equals()andItem#hashCode()is properly implemented as per their contracts.If you can't guarantee that, then you'd better use a
Map<RowId, Boolean>instead whereRowIdrepresents the type of the row identifier. Let's take an example that you've aItemobject whose identifier propertyidis aLong: