作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经创建了复选框。当我选择多个复选框时,我怎样才能获得那些多个选中的复选框值?我的代码是:
<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}",因此它返回单个值。
最佳答案
value
的 <h:selectManyXxx>
组件需要指向一个数组或List
与 itemValue
属于同一类型.假设它是 Long
, 然后它需要绑定(bind)到 Long[]
或 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 将设置 String
List
中的值这最终只会导致 ClassCaseException
秒。因此:
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-2 - 如何在 jsf 中获取多选复选框值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11065427/
我是一名优秀的程序员,十分优秀!