PrimeFaces控件selectManyCheckbox值类型出错
Posted on: 2014-02-23, Last modified: 2015-07-31, View: 3157
Posted on: 2014-02-23, Last modified: 2015-07-31, View: 3157
使用PrimeFaces控件selectManyCheckbox的时候会遇到得到的选择值列表类型错误的问题,有可能会报“Value is not a valid option.”的错误,JSF的同名控件也有一样的问题,问题代码如下:
<pf:selectManyCheckbox id="selectCheckbox" layout="pageDirection" value="#{testBean.selectValueList}"> <f:selectItems value="#{testBean.valueDateList}" var="data" itemLabel="#{data.getName()}" itemValue="#{data.code}"/> </pf:selectManyCheckbox>
java代码里selectValueList声明为:
private List<Integer> selectValueList = new ArrayList<>();
这时候就会发现ManageBean中的得到的值被转换为List <String>类型,原因是JSF EL在运行时不能解析List泛型,导致类型丢失,解决办法有两种:
1. 声明选择值为数组,就可以解决类型丢失问题
private Integer[] selectValueList;
2. 在控件上添加Converter,把String类型的元素转化成需要的类型
<pf:selectManyCheckbox id="selectCheckbox" layout="pageDirection" value="#{testBean.selectValueList}" converter="javax.faces.Integer"> <f:selectItems value="#{testBean.valueDateList}" var="data" itemLabel="#{data.getName()}" itemValue="#{data.code}"/> </pf:selectManyCheckbox>