如何从selectManyListbox/selectManyMenu/selectManyCheckbox获取所有选定的值? [英] How to get all the selected values from 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类型之一(String
,Number
或Boolean
),则还必须提供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.
}
// ...
}
另请参见:
- 我们的
selectOneMenu
Wiki页面 - >如何从数据库中填充h:selectOneMenu的选项?
- Our
selectOneMenu
wiki page - How to populate options of h:selectOneMenu from database?
See also:
这篇关于如何从selectManyListbox/selectManyMenu/selectManyCheckbox获取所有选定的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!