How to create an array in JSF EL?

Sandro picture Sandro · May 22, 2012 · Viewed 12.2k times · Source

I want create an array in JSF EL. How can I do that? Is it even possible?

To illustrate what I am trying:

<rich:pickList addAllText="" addText="" removeAllText="" removeText="">
    <f:selectItems value="#{'Test', 'TestTest', 'TestTestTest'}" />
</rich:pickList>

Answer

BalusC picture BalusC · May 22, 2012

If you're on EL 3.0 or newer, you can construct collections directly in EL.

<f:selectItems value="#{['Test','TestTest','TestTestTest']}" />

If you're not on EL 3.0 yet, you could solve this particular case with a fn:split() trick.

<html ... xmlns:fn="http://java.sun.com/jsp/jstl/functions">
...
<f:selectItems value="#{fn:split('Test,TestTest,TestTestTest', ',')}" />

Either way, this requires a minimum of JSF 2.0 for the support of List<T> in <f:selectItems>.