Jsf works with collection / map

Can I bind my h: dataTable / rich: dataTable using some map? I found that h: dataTable can only work with a List object, and deleting in a List can be very difficult.

+4
source share
2 answers

Yes, that's right. dataTable, ui: repeat and friends only work with lists.

You can simply add a managed bean method that puts map.keySet () or map.values ​​() in the list, depending on which you want to iterate over.

Usually, when I want to iterate over a map from a JSF view, I do something like

<h:dataTable value="#{bean.mapKeys}" var="key"> <h:outputText value="#{bean.map[key]}"/> </h:dataTable> 

with managed property bean

 class Bean { public List<T> mapKeys() { return new ArrayList<T>(map.keySet()); } } 

or something like that.

Of course, this makes sense if you use something like TreeMap or LinkedHashMap that keeps order.

+4
source

If you want to have only one method in your bean support that provides a map, you can do something like this:

 class Bean { public Map<T,U> getMap() { return yourMap; } } 

and use this in a jsf view

 <h:dataTable value="#{bean.map.keySet().toArray()}" var="key"> <h:outputText value="#{bean.map[key]}"/> </h:dataTable> 

This converts the key set to an array, which can be repeated using datatable. Using the expression "()" requires Unified Expression Language 2 (Java EE 6).

+4
source

All Articles