开发者

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 开发者_如何学Goa 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. 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:

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 (i.e. not a String, Number or Boolean for which EL has already builtin coercions), then you'll have to create a Converter for this. More details can be found in this blog article.


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

and in the managed bean:

private List list;

(with the appropriate getter and setter, and if possible - using generics)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜