如何从数据库中填充selectManyListbox

时间:2011-04-24 21:17:09

标签: jsf

我想知道如何从数据库填充h:selectManyListbox,而不是静态选项。

1 个答案:

答案 0 :(得分:1)

<f:selectItems>与返回List<SelectItem>的属性结合使用,或者当您已使用JSF 2.0时,List<SomeObject>

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

您可以在bean的构造函数或@PostConstruct方法中加载数据库中的项目。

public class Bean {

    private List<String> selectedItems;
    private List<SelectItem> selectItems;

    public Bean() {
        selectItems = new ArrayList<SelectItem>();

        // Fill select items during Bean initialization/construction.
        // Below is just an example, you could replace this by getting a list
        // of some objects from DB and creating new items in a loop.
        selectItems.add(new SelectItem("value1", "label1"));
        selectItems.add(new SelectItem("value2", "label2"));
        selectItems.add(new SelectItem("value3", "label3"));
    }

    // Getters, etc
}