我创建了复选框。当我选择多个复选框时,如何获得这些多个选中的复选框值?我的代码是:
<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}”,因此它返回单个值。
答案 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会设置String
值List
最终只会导致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>