1

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
user3280015
  • 279
  • 2
  • 10

1 Answers1

1

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Although this works wouldn't it be better to not have this kind of 'conplexity' in EL for peformance reasons and do it lazy in a bean? – Kukeltje Mar 06 '15 at 00:42
  • @Kukeltje: I'm not seeing performance problems here. However, design wise model values surely belong in the model. But the OP asked so and it's not impossible :) – BalusC Mar 06 '15 at 08:24