我正在使用PickList,它包含我的bean列表Country [Country - >地区 - >公司]。问题是,当我将picklist.getTarget()值转换为Country时,它会抛出类转换异常。我也在使用Country转换器。我不知道是什么问题?
这是我的代码,
<p:pickList id="pickListId" required="true"
value="#{countryBean.countryDualModel}" var="avlCountry"
itemValue="#{avlCountry}"
itemLabel="#{avlCountry.region.company.companyName}"
converter="#{countryDualListConverter}" showCheckbox="true"
showSourceFilter="true" showTargetFilter="true"
filterMatchMode="contains">
<p:ajax event="transfer" listener="#{countryBean.transferEvent}"></p:ajax>
<p:column style="width:75%;">#{avlCountry.region.company.companyName}</p:column>
</p:pickList>
@FacesConverter(value="bizEntityDualListConverter")
public class BizEntityDualListConverter implements Converter {
@Override
public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {
Object ret = null;
if (arg1 instanceof PickList) {
Object dualList = ((PickList) arg1).getValue();
@SuppressWarnings("unchecked")
DualListModel<Country> dl = (DualListModel<Country>) dualList;
for (Object o : dl.getSource()) {
String id = "" + ((Country) o).getRegion().getCompanyId();
if (arg2.equals(id)) {
ret = o;
break;
}
}
for(Object o : dl.getTarget()){
String id = "" + ((Country) o).getRegion().getCompanyId();
if(arg2.equals(id)){
ret = o;
break;
}
}
if (ret == null)
for (Object o : dl.getTarget()) {
String id = "" + ((Country) o).getRegion().getCompanyId();
if (arg2.equals(id)) {
ret = o;
break;
}
}
}
return ret;
}
@Override
public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {
String str = "";
if (arg2 instanceof Country) {
str = "" + ((Country) arg2)..getRegion().getCompanyId();
}
return str;
}
Popuate DualListModel:
DualListModel countryDualModel = new DualListModel&lt;&gt;(sourceCountryList,targetCountryList);
public void transferEvent(TransferEvent event){
int size = countryDualModel .getTarget().size();
for(Country ctry : (Country) countryDualModel .getTarget()){
//
}
}
//当它到来时,它抛出类强制转换异常,java.lang.ClassCastException:java.lang.String不能强制转换为com.test.Country
答案 0 :(得分:0)
缺少转换器名称converter="#{countryDualListConverter}"
?
更改为converter="#{bizEntityDualListConverter}"
。
您也可以使用Ominifaces's ListIndexConverter。这是一个很好的JSF Utility
。