문제

Can I bind my h:dataTable/rich:dataTable with some Map? I found that h:dataTable can work only with List object and deleting in List can be very heavy.

도움이 되었습니까?

해결책

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

You can just add a managed bean method that puts map.keySet() or map.values() into a list depending on which you want to iterate over.

Typically when I want to iterate 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 bean property

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

or something like that.

Of course, this makes the most sense if you're using something like TreeMap or LinkedHashMap that preserves ordering.

다른 팁

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

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

and use this in your JSF view

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

This converts the key set into an array, which can be iterated by the datatable. Using the "()"-expression requires Unified Expression Language 2 (Java EE 6).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top