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

How to collect all selected values ​​from UISelectMany components, such as h: selectManyListbox, h: selectManyMenu, h: selectManyCheckbox, p: selectManyListbox, p: selectManyMenu, p: selectManyCheckbox, etc. in bean support?

If someone can help with an example, this will really help.

+3
source share
1 answer

As with any other input component, just bind its attribute valueusing the managed bean. It can match with Listor an array of the same type of values ​​that you used in f:selectItem(s). If the value type is not one of the standard EL types ( String, Numberor Boolean), then you must also supply Converter.

Here is an example with a type value 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 already set at that point.
    }

    // ...
}

See also:

+12
source

Source: https://habr.com/ru/post/1778583/


All Articles