As with every other input component, just bind its value
attribute with a managed bean property. It can map to a List
or an array of the same value type as you've used in f:selectItem(s)
. If the value type is not one of the standard EL types (String
, Number
or Boolean
), then you have to supply a Converter
as well.
Here's an example with a value type of String
:
<h:selectManyListbox value="#{bean.selectedItems}">
<f:selectItems value="#{bean.availableItems}" />
</h:selectManyListbox>
<h:commandButton value="Submit" action="#{bean.submit}" />
with
public class Bean {
private Map<String, String> availableItems; // +getter (no setter necessary)
private List<String> selectedItems; // +getter +setter
@PostConstruct
public void init() {
availableItems = new LinkedHashMap<String, String>();
availableItems.put("Foo label", "foo");
availableItems.put("Bar label", "bar");
availableItems.put("Baz label", "baz");
}
public void submit() {
System.out.println(selectedItems); // It's already set at that point.
}
// ...
}
See also:
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…