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


luni, 12 octombrie 2015

[OmniFaces utilities (2.0)] Convert a Set<E> to a List<E>

[OmniFaces utilities] The setToList() function converts a Set<E> to a List<E>. Useful when you want to iterate over a Set in for example <ui:repeat>.


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

HashSet<Players> dataHashSet = new HashSet<>();
TreeSet<Players> dataTreeSet = new TreeSet<>();
LinkedHashSet<Players> dataLinkedHashSet = new LinkedHashSet<>();  

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

<h:dataTable value="#{of:setToList(playersBean.dataHashSet)}" var="t">

<h:dataTable value="#{of:setToList(playersBean.dataTreeSet)}" var="t">

<h:dataTable value="#{of:setToList(playersBean.dataLinkedHashSet)}" var="t">

Starting with JSF 2.2 the <h:dataTable> support Set directly, so you don't need of:setToList() anymore. You can simply write:

<h:dataTable value="#{playersBean.dataHashSet}" var="t">

<h:dataTable value="#{playersBean.dataTreeSet}" var="t">

<h:dataTable value="#{playersBean.dataLinkedHashSet}" var="t">

Well, <ui:repeat> still cannot directly use Set in JSF 2.2, so you can use of:setToList() or, via EL 2.2, toArray() method. Check the below examples for dataHashSet:

//via of:setToList()
<ui:repeat value="#{of:setToList(playersBean.dataHashSet)}" var="t">

//via toArray()
<ui:repeat value="#{playersBean.dataHashSet.toArray()}" var="t">

Niciun comentariu :

Trimiteți un comentariu


Postări populare

OmniFaces/JSF Fans

Follow by Email

Visitors Starting 4 September 2015

Locations of Site Visitors