如何在jsf中获得多选复选框值? [英] How can I get multiselected checkbox value in jsf?

查看:274
本文介绍了如何在jsf中获得多选复选框值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have created checkboxes. When I selected multiple checkboxes then how can I get those multiple selected checkboxes value? my code is:

<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}",因此它返回单个值.

I have taken value="#{adcreateBean.editionID}" ,so it returns single value.

推荐答案

<h:selectManyXxx>组件的value必须指向与itemValue完全相同类型的数组或List.假设它是Long,则需要将其绑定到Long[]List<Long>.

The value of the <h:selectManyXxx> component needs to point to an array or List of the very same type as the itemValue. Assuming that it's Long, then it needs to be bound to a Long[] or List<Long>.

例如

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会在List中设置String值,这会最终只会导致ClassCaseException.因此:

If you prefer a List<Long>, then you should explicitly supply a converter for the Long type, because generic types are erased during runtime and without a converter EL would set String values in the List which would ultimately only result in ClassCaseExceptions. Thus so:

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>

这篇关于如何在jsf中获得多选复选框值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆