Search code examples
jsfelliteralsselectmanycheckbox

How can I pass a list literal to f:selectItems?


For example:

<h:selectManyCheckbox
    id="literalOptions"
    value="firstOption">
    <f:selectItems value="[firstOption, secondOption, thirdOption]"/>
</h:selectManyCheckbox>

this doesn't work, but you get the idea?

I want to pass literal (string-type) list options (not necessarily but possibly retrieved from a bean property as a String), the [a,b,c] list-syntax probably isn't correct, but which is?

Or, alternatively, how can I pass a list literal to a custom component I create that passes this literal to f:selectItems which I use internally in my custom component? That is, how can I create a custom component that accepts such a literal for ad hoc definition of list values by the component user.


Solution

  • If you're on Java EE 6, use JSTL fn:split() trick.

    <html ... xmlns:fn="http://java.sun.com/jsp/jstl/functions">
    ...
    <h:selectManyCheckbox value="firstOption">
        <f:selectItems value="#{fn:split('firstOption,secondOption,thirdOption', ',')}"/>
    </h:selectManyCheckbox>
    

    If you're on Java EE 7, use EL 3.0 collection literal.

    <h:selectManyCheckbox value="firstOption">
        <f:selectItems value="#{['firstOption', 'secondOption', 'thirdOption']}"/>
    </h:selectManyCheckbox>
    

    You was close, you just had to quote the string values and to put the whole in #{...}.

    Note that specifying a literal in <h:selectManyCheckbox value> would fail with a PropertyNotWritableException on submit, but that's a different problem.