我正在使用包含我的 bean 列表 Country[Country -> Region -> Company] 的 PickList。问题是当我将 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:
填充 DualListModel:
DualListModel countryDualModel = new 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
最佳答案
缺少转换器名称converter="#{countryDualListConverter}"
?
更改为 converter="#{bizEntityDualListConverter}"
。
您还可以使用Ominifaces's ListIndexConverter 。这是一个很好的 JSF 实用程序
。
关于java - PrimeFaces 选择列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22085596/