我使用的是PickList,它包含我的bean列表CountryCountry -> Region -> Company.The,当我将picklist.getTarget()值转换到Country时,它会抛出类强制转换异常。我也在用乡村转换器。我不知道有什么问题吗?
这是我的密码,
XHTML:
<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;
}CountryBean:
Popuate DualListModel:
DualListModel countryDualModel =新DualListModel<>(sourceCountryList,targetCountryList);
OnTransfer侦听器:
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
发布于 2014-02-28 04:33:07
缺少转换器名converter="#{countryDualListConverter}"?
改为converter="#{bizEntityDualListConverter}"。
您也可以使用欧米诺的ListIndexConverter。这是一个不错的JSF Utility。
https://stackoverflow.com/questions/22085596
复制相似问题