如何在jsf中获取多选复选框值?

时间:2012-06-16 17:05:39

标签: jsf-2

我创建了复选框。当我选择多个复选框时,如何获得这些多个选中的复选框值?我的代码是:

<h:selectManyCheckbox id="chkedition" value="#{adcreateBean.editionID}" layout="lineDirection" styleClass="nostyle">
<f:selectItems value="#{adcreateBean.editions}" var="item" itemLabel="#{item.editionName}" itemValue="#{item.editionID}"/>
</h:selectManyCheckbox>

我已经使用了value =“#{adcreateBean.editionID}”,因此它返回单个值。

1 个答案:

答案 0 :(得分:3)

value组件的<h:selectManyXxx>需要指向与List完全相同的数组或itemValue。假设它是Long,那么它需要绑定到Long[]List<Long>

E.g。

private Long[] selectedEditionIds; // +getter +setter
private List<Edition> availableEditions; // +getter

<h:selectManyCheckbox value="#{bean.selectedEditionIds}">
    <f:selectItems value="#{bean.availableEditions}" var="edition" itemLabel="#{edition.name}" itemValue="#{edition.id}" />
</h:selectManyCheckbox>

如果您更喜欢List<Long>,那么您应该明确地为Long类型提供转换器,因为泛型类型在运行时被擦除而没有转换器EL会设置StringList最终只会导致ClassCaseException s。因此:

private List<Long> selectedEditionIds; // +getter +setter
private List<Edition> availableEditions; // +getter

<h:selectManyCheckbox value="#{bean.selectedEditionIds}" converter="javax.faces.Long">
    <f:selectItems value="#{bean.availableEditions}" var="edition" itemLabel="#{edition.name}" itemValue="#{edition.id}" />
</h:selectManyCheckbox>