How to get all the selected values from selectManyListbox / selectManyMenu / selectManyCheckbox?

こ雲淡風輕ζ 提交于 2019-11-27 15:43:28
BalusC

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:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!