我在JSF中有一个selectmanyListbox组件,现在我想将所选数据存储到List中。怎么做?
答案 0 :(得分:6)
与每个UIInput
组件一样,您只需将value
属性与辅助bean的属性绑定即可。因此,所以:
<h:form>
<h:selectManyListbox value="#{bean.selectedItems}">
<f:selectItems value="#{bean.selectItems}" />
</h:selectManyListbox>
<h:commandButton value="submit" action="#{bean.submit}" />
</h:form>
以下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.
}
}
如果您想将非标准对象用作SelectItem
值(即不是EL已经内置强制的String
,Number
或Boolean
,那么你必须为此创建一个Converter
。有关详细信息,请参阅this blog article。
答案 1 :(得分:2)
<h:selectManyListBox value="#{managedBean.list}">
并在托管bean中:
private List list;
(使用适当的getter和setter,如果可能的话 - 使用泛型)