如何从selectManyListbox/selectManyMenu/selectManyCheckbox获取所有选定的值? [英] How to get all the selected values from selectManyListbox / selectManyMenu / selectManyCheckbox?

查看:167
本文介绍了如何从selectManyListbox/selectManyMenu/selectManyCheckbox获取所有选定的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从UISelectMany组件(例如h:selectManyListbox,h:selectManyMenu,h:selectManyCheckbox,p:selectManyListbox,p:selectManyMenu,p: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.

推荐答案

与其他所有输入组件一样,只需将其value属性与托管bean属性绑定即可.它可以映射到List或与f:selectItem(s)中使用的值类型相同的数组.如果值类型不是标准EL类型之一(StringNumberBoolean),则还必须提供Converter.

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.

下面是一个值类型为String的示例:

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}" />

使用

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.
    }

    // ...
}

另请参见:

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆