-
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()
.
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)
.
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" Faces utility library, feel free to post a feature request.
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.
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("Thu, 21 Nov 2024 00:24:26 -0600");
<h3>Using <code>of:xxxToList/Model()</code> functions</h3>
<p>
Convert <code>Set<E></code> to <code>List<E></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<E></code> to <code>List<E></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<E></code> to <code>DataModel<E></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<E></code> to <code>DataModel<E></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<K, V></code> to <code>List<Entry<K, V>></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>
package org.omnifaces.showcase.functions;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import jakarta.annotation.PostConstruct;
import jakarta.enterprise.context.RequestScoped;
import jakarta.inject.Named;
@Named
@RequestScoped
public class ConvertersBean {
private String[] array;
private Set<String> set;
private Map<String, String> map;
private String[] bigArray;
private List<String> bigList;
@PostConstruct
public void init() {
array = new String[] { "item1", "item2", "item3" };
set = new LinkedHashSet<>(3);
set.add("item1");
set.add("item2");
set.add("item3");
map = new LinkedHashMap<>(3);
map.put("key1", "value1");
map.put("key2", "value2");
map.put("key3", "value3");
bigArray = new String[] { "item1", "item2", "item3", "item4", "item5", "item6", "item7", "item8", "item9" };
bigList = Arrays.asList(bigArray);
}
public String[] getArray() {
return array;
}
public Set<String> getSet() {
return set;
}
public Map<String, String> getMap() {
return map;
}
public String[] getBigArray() {
return bigArray;
}
public List<String> getBigList() {
return bigList;
}
public Iterable<Integer> getIterable() {
return new TestIterable();
}
private static class TestIterable implements Iterable<Integer> {
@Override
public Iterator<Integer> iterator() {
return new Iterator<Integer>() {
int index = 0;
@Override
public boolean hasNext() {
return index < 3;
}
@Override
public Integer next() {
return index++;
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
}
}
}
VDL documentation
of:setToList.fn
of:mapToList.fn
of:joinArray.fn
of:joinCollection.fn
of:joinMap.fn
of:splitArray.fn
of:splitList.fn
of:toJson.fn