简体   繁体   English

如何将selectManyListbox中选择的数据存储到JSF中的List中?

[英]How to store the data's selected in a selectManyListbox into a List in JSF?

I am having a selectmanyListbox component in my JSF, now i want to store the selected data's into a List. 我的JSF中有一个selectmanyListbox组件,现在我想将所选数据存储到一个List中。 How to do this? 这该怎么做?

As with every UIInput component, you just have to bind the value attribute with a property of the backing bean. 与每个UIInput组件一样,您只需要将value属性与后备bean的属性绑定。 Thus, so: 因此,这样:

<h:form>
    <h:selectManyListbox value="#{bean.selectedItems}">
        <f:selectItems value="#{bean.selectItems}" />
    </h:selectManyListbox>
    <h:commandButton value="submit" action="#{bean.submit}" />
</h:form>

with the following in Bean class: Bean类中具有以下内容:

private List<String> selectedItems; // + getter + setter
private List<SelectItem> selectItems; // + getter only

public Bean() {
    // Fill select items during Bean initialization/construction.
    selectItems = new ArrayList<SelectItem>();
    selectItems.add(new SelectItem("value1", "label1"));
    selectItems.add(new SelectItem("value2", "label2"));
    selectItems.add(new SelectItem("value3", "label3"));
}

public void submit() {
    // JSF has already put selected items in `selectedItems`.
    for (String selectedItem : selectedItems) {
        System.out.println("Selected item: " + selectedItem); // Prints value1, value2 and/or value3, depending on selection.
    }
}

If you want to use non-standard objects as SelectItem value (ie not a String , Number or Boolean for which EL has already builtin coercions), then you'll have to create a Converter for this. 如果要使用非标准对象作为SelectItem值(即,不是EL已内置强制ConverterStringNumberBoolean ),则必须为此创建一个Converter More details can be found in this blog article . 可以在此博客文章中找到更多详细信息。

<h:selectManyListBox value="#{managedBean.list}">

and in the managed bean: 并在托管bean中:

private List list;

(with the appropriate getter and setter, and if possible - using generics) (使用适当的getter和setter,并在可能的情况下使用泛型)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM