• cdi
  • components
  • contexts
  • converters
  • el
  • eventlisteners
  • exceptionhandlers
  • facesviews
  • filters
  • functions
  • managedbeans
  • push
  • resourcehandlers
  • scripts
  • servlets
  • taghandlers
  • utils
  • validators
  • viewhandlers
 - 
  • Arrays
  • Converters
  • Dates
  • Numbers
  • Objects
  • Request
  • Strings

Available since OmniFaces 1.0

Collection of EL functions for data conversion: of:iterableToList() (with alternative of:iterableToModel), of:setToList(), of:mapToList(), of:joinArray(), of:joinCollection(), of:joinMap(), of:splitArray(), of:splitList(), and of:toJson().

Regarding the of:xxxToList() functions; <ui:repeat> (and <h:dataTable>) doesn't support Iterable (such as Set) and Map directly, so those functions may be handy for them. If however EL 2.2 is used, then e.g. #{bean.set.toArray()} (for Collection types) and #{bean.map.entrySet().toArray()} could be used instead. But if EL 2.2 is not supported or for a general Iterable the provided EL functions can be used.

The of:joinXxx() functions basically joins the elements of the array, collection or map to a string using the given separator. This may be helpful if you want to display the contents of a collection as a commaseparated string without the need for an <ui:repeat>.

The of:splitXxx() functions basically splits an array or list into an array of subarrays or list of sublists of the given fragment size. This may be helpful if you want to create a two-dimensional matrix of a fixed width based on a single-dimensional array or list.

The of:toJson() function encodes any object to a string in JSON format according the rules of Json.encode(Object).

Note that since JSF 2.2 it should be possible to use #{bean.set} directly in <h:dataTable>, but not in <ui:repeat>. The of:setToList() thus remains useful for JSF 2.0 and 2.1 in all cases, but is not needed for <h:dataTable> in JSF 2.2.

Feature request

If you know more useful methods/functions which you think should be added to this OmniFaces utility class so that everyone can benefit from a "standard" JSF utility library, feel free to post a feature request.

Demo

Using of:xxxToList/Model() functions

Convert Set<E> to List<E> using #{of:setToList()}: item1, item2, item3

Convert Iterable<E> to List<E> using #{of:iterableToList()}: 0, 1, 2

(Alternative) Convert Iterable<E> to DataModel<E> using #{of:iterableToModel()}: 0, 1, 2

(Alternative) Convert Set<E> to DataModel<E> using #{of:iterableToModel()}: item1, item2, item3

Convert Map<K, V> to List<Entry<K, V>> using #{of:mapToList()}: key1=value1, key2=value2, key3=value3


Just to show EL 2.2 ability to invoke non-getter methods, so that of:xxxToList() are not necessary

Use #{bean.set.toArray()} directly: item1, item2, item3

Use #{bean.map.entrySet().toArray()} directly: key1=value1, key2=value2, key3=value3


Using of:joinXxx() functions

Join array to string using #{of:joinArray()}: item1, item2, item3

Join collection to string using #{of:joinCollection()}: item1, item2, item3

Join map to string using #{of:joinMap()}: key1=value1, key2=value2, key3=value3


Using of:splitXxx() functions

Split an array of 9 elements into a 3-column div.

item1 item2 item3
item4 item5 item6
item7 item8 item9

Split a list of 9 elements into 3 lists.

  • item1
  • item2
  • item3
  • item4
  • item5
  • item6
  • item7
  • item8
  • item9

Encode any object to JSON

The following should represent valid JavaScript variables:

 var array = ["item1","item2","item3"];
 var collection = ["item1","item2","item3"];
 var map = {"key1":"value1","key2":"value2","key3":"value3"};
 var exampleEntities = [{"id":1,"value":"Amsterdam"},{"id":2,"value":"Frankfurt"},{"id":3,"value":"London"}];
 var serverDate = new Date("Fri, 24 Feb 2017 14:46:13 GMT");
Demo source code
<h3>Using <code>of:xxxToList/Model()</code> functions</h3>
<p>
    Convert <code>Set&lt;E&gt;</code> to <code>List&lt;E&gt;</code> using <code>\#{of:setToList()}</code>:
    <strong>
        <ui:repeat value="#{of:setToList(convertersBean.set)}" var="item" varStatus="loop">
            #{item}#{!loop.last ? ', ' : ''}
        </ui:repeat>
    </strong>
</p>
<p>
    Convert <code>Iterable&lt;E&gt;</code> to <code>List&lt;E&gt;</code> using <code>\#{of:iterableToList()}</code>:
    <strong>
        <ui:repeat value="#{of:iterableToList(convertersBean.iterable)}" var="item" varStatus="loop">
            #{item}#{!loop.last ? ', ' : ''}
        </ui:repeat>
    </strong>
</p>
<p>
    (Alternative) Convert <code>Iterable&lt;E&gt;</code> to <code>DataModel&lt;E&gt;</code> using <code>\#{of:iterableToModel()}</code>:
    <strong>
        <ui:repeat value="#{of:iterableToModel(convertersBean.iterable)}" var="item" varStatus="loop">
            #{item}#{!loop.last ? ', ' : ''}
        </ui:repeat>
    </strong>
</p>
<p>
    (Alternative) Convert <code>Set&lt;E&gt;</code> to <code>DataModel&lt;E&gt;</code> using <code>\#{of:iterableToModel()}</code>:
    <strong>
        <ui:repeat value="#{of:iterableToModel(convertersBean.set)}" var="item" varStatus="loop">
            #{item}#{!loop.last ? ', ' : ''}
        </ui:repeat>
    </strong>
</p>
<p>
    Convert <code>Map&lt;K, V&gt;</code> to <code>List&lt;Entry&lt;K, V&gt;&gt;</code> using <code>\#{of:mapToList()}</code>:
    <strong>
        <ui:repeat value="#{of:mapToList(convertersBean.map)}" var="entry" varStatus="loop">
            #{entry.key}=#{entry.value}#{!loop.last ? ', ' : ''}
        </ui:repeat>
    </strong>
</p>

<hr />

<h3>Just to show EL 2.2 ability to invoke non-getter methods, so that <code>of:xxxToList()</code> are not necessary</h3>
<p>
    Use <code>\#{bean.set.toArray()}</code> directly:
    <strong>
        <ui:repeat value="#{convertersBean.set.toArray()}" var="item" varStatus="loop">
            #{item}#{!loop.last ? ', ' : ''}
        </ui:repeat>
    </strong>
</p>
<p>
    Use <code>\#{bean.map.entrySet().toArray()}</code> directly:
    <strong>
        <ui:repeat value="#{convertersBean.map.entrySet().toArray()}" var="entry" varStatus="loop">
            #{entry.key}=#{entry.value}#{!loop.last ? ', ' : ''}
        </ui:repeat>
    </strong>
</p>

<hr />

<h3>Using <code>of:joinXxx()</code> functions</h3>
<p>
    Join array to string using <code>\#{of:joinArray()}</code>: 
    <strong>#{of:joinArray(convertersBean.array, ', ')}</strong>
</p>
<p>
    Join collection to string using <code>\#{of:joinCollection()}</code>:
    <strong>#{of:joinCollection(convertersBean.set, ', ')}</strong>
</p>
<p>
    Join map to string using <code>\#{of:joinMap()}</code>: 
    <strong>#{of:joinMap(convertersBean.map, '=', ', ')}</strong>
</p>

<hr />

<h3>Using <code>of:splitXxx()</code> functions</h3>
<p>
    Split an array of 9 elements into a 3-column div.
</p>
<div>
    <ui:repeat value="#{of:splitArray(convertersBean.bigArray, 3)}" var="subArray">
        <div>
            <ui:repeat value="#{subArray}" var="item">
                <span>#{item}</span>
            </ui:repeat>
        </div>
    </ui:repeat>
</div>
<p>
    Split a list of 9 elements into 3 lists.
</p>
<ui:repeat value="#{of:splitList(convertersBean.bigList, 3)}" var="subList">
    <ul>
        <ui:repeat value="#{subList}" var="item">
            <li>#{item}</li>
        </ui:repeat>
    </ul>
</ui:repeat>

<hr />

<h3>Encode any object to JSON</h3>
<p>
    The following should represent valid JavaScript variables:
</p>
<pre class="prettyprint"><code class="lang-javascript"> var array = #{of:toJson(convertersBean.array)};
 var collection = #{of:toJson(convertersBean.set)};
 var map = #{of:toJson(convertersBean.map)};
 var exampleEntities = #{of:toJson(selectItemsBean.exampleEntities)};
 var serverDate = new Date(#{of:toJson(now)});</code></pre>