PrimeFaces PickList

时间:2014-02-28 03:27:34

标签: java primefaces jsf-2

我正在使用PickList,它包含我的bean列表Country [Country - >地区 - >公司]。问题是,当我将picklist.getTarget()值转换为Country时,它会抛出类转换异常。我也在使用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 = new DualListModel&lt;&gt;(sourceCountryList,targetCountryList);

OnTransfer Listener:

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

1 个答案:

答案 0 :(得分:0)

缺少转换器名称converter="#{countryDualListConverter}"
更改为converter="#{bizEntityDualListConverter}"

您也可以使用Ominifaces's ListIndexConverter。这是一个很好的JSF Utility