Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
853 views
in Technique[技术] by (71.8m points)

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

How do I collect all selected values from UISelectMany components such as h:selectManyListbox, h:selectManyMenu, h:selectManyCheckbox, p:selectManyListbox, p:selectManyMenu, p:selectManyCheckbox, etc in backing bean?

If someone can help with an example, that would really help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...