My JSF Books/Videos My JSF Tutorials OmniFaces/JSF PPTs
JSF 2.3 Tutorial
JSF Caching Tutorial
JSF Navigation Tutorial
JSF Scopes Tutorial
JSF Page Author Beginner's Guide
OmniFaces 2.3 Tutorial Examples
OmniFaces 2.2 Tutorial Examples
JSF Events Tutorial
OmniFaces Callbacks Usages
JSF State Tutorial
JSF and Design Patterns
JSF 2.3 New Features (2.3-m04)
Introduction to OmniFaces
25+ Reasons to use OmniFaces in JSF
OmniFaces Validators
OmniFaces Converters
JSF Design Patterns
Mastering OmniFaces
Reusable and less-verbose JSF code

My JSF Resources ...

Java EE Guardian
Member of JCG Program
Member MVB DZone
Blog curated on ZEEF
OmniFaces is an utility library for JSF, including PrimeFaces, RichFaces, ICEfaces ...

.

.

.

.

.

.

.

.


[OmniFaces Utilities] - Find the right JSF OmniFaces 2 utilities methods/functions

Search on blog

Petition by Java EE Guardians

Twitter

marți, 13 octombrie 2015

[OmniFaces utilities (2.0)] Convert a Map<K, V> to a List<Map.Entry<K, V>>


[OmniFaces utilities] The mapToList() function converts a Map<K, V> to a List<Map.Entry<K, V>>. Useful when you want to iterate over a Map in for example <ui:repeat>. Each of the entries has the usual getKey() and getValue() methods.

Function:
Usage:

The of:mapToList() is very useful for JSF 2.2/2.0/2.1 where <h:dataTable> (and <ui:repeat>) doesn't support Map directly. For example, via this method you can easily use #{fooBean.map} in <h:dataTable>:

HashMap<String, Players> dataHashMap = new HashMap<>();
TreeMap<String, Players> dataTreeMap = new TreeMap<>();
LinkedHashMap<String, Players> dataLinkedHashMap = new LinkedHashMap<>();  

Now, for JSF 2.2/2.1/2.0, you can do this:

<h:dataTable value="#{of:mapToList(playersBean.dataHashMap)}" var="t">
 #{t.key} #{t.value}
</h:dataTable>

<h:dataTable value="#{of:mapToList(playersBean.dataTreeMap)}" var="t">
 #{t.key} #{t.value}
</h:dataTable>

<h:dataTable value="#{of:mapToList(playersBean.dataLinkedHashMap)}" var="t">
 #{t.key} #{t.value}
</h:dataTable>

However, starting with JSF 2.2, UIData (e.g.  <h:dataTable>) support Set directly, so you don't need of:mapToList() if you prefer entrySet() (you may want to consider keySet() and values() also):

<h:dataTable value="#{playersBean.dataHashMap.entrySet()}" var="t">
 #{t.key} #{t.value}
</h:dataTable>

<h:dataTable value="#{playersBean.dataTreeMap.entrySet()}" var="t">
 #{t.key} #{t.value}
</h:dataTable>

<h:dataTable value="#{playersBean.dataLinkedHashMap.entrySet()}" var="t">
 #{t.key} #{t.value}
</h:dataTable>

When Set is not directly supported, but, if EL 2.2 is available, then you can use toArray() method, so you don't need of:mapToList():

<h:dataTable value="#{playersBean.dataHashMap.entrySet().toArray()}" var="t">
 #{t.key} #{t.value}
</h:dataTable>

<h:dataTable value="#{playersBean.dataTreeMap.entrySet().toArray()}" var="t">
 #{t.key} #{t.value}
</h:dataTable>

<h:dataTable value="#{playersBean.dataLinkedHashMap.entrySet().toArray()}" var="t">
 #{t.key} #{t.value}
</h:dataTable>

Well, speaking about <ui:repeat>, you should know that Map is not supported directly in JSF 2.2/2.1/2.0. In such cases, you can use of:mapToList():

<ui:repeat value="#{of:mapToList(playersBean.dataHashMap)}" var="t">
 <h:outputText value="key:#{t.key} value:#{t.value}" />
</ui:repeat>

<ui:repeat value="#{of:mapToList(playersBean.dataTreeMap)}" var="t">
 <h:outputText value="key:#{t.key} value:#{t.value}" />
</ui:repeat>

<ui:repeat value="#{of:mapToList(playersBean.dataLinkedHashMap)}" var="t">
 <h:outputText value="key:#{t.key} value:#{t.value}" />
</ui:repeat>

Starting with JSF 2.2, UIData (e.g.  <h:dataTable>) support Set directly, but <ui:repeat> doesn't! So, if EL 2.2 is available, you can use toArray() method. Below, you can see four approaches exemplified on dataHashMap:

<ui:repeat value="#{playersBean.dataHashMap.entrySet().toArray()}" var="t">
 <h:outputText value="key:#{t.key} value:#{t.value}" />
</ui:repeat>

<ui:repeat value="#{playersBean.dataHashMap.keySet().toArray()}" var="t">
 <h:outputText value="key:#{t} value:#{playersBean.dataHashMap.get(t)}" />
</ui:repeat>

<ui:repeat value="#{playersBean.dataHashMap.values().toArray()}" var="t">
 <h:outputText value="#{t}" />
</ui:repeat>

<ui:repeat value="#{playersBean.dataHashMap.entrySet()}" var="t">
 <ui:repeat value="#{t.toArray()}" var="q">
  <h:outputText value="key:#{q.key} value:#{q.value}" />
 </ui:repeat>

Niciun comentariu :

Trimiteți un comentariu

JSF BOOKS COLLECTION

Postări populare

OmniFaces/JSF Fans

Visitors Starting 4 September 2015

Locations of Site Visitors