2013-08-19 134 views
0

創建多維枚舉的選擇組件模型在我的網頁類:在掛毯

public SelectModel getCountryListEnum() { 
    return new EnumSelectModel(CountryListEnum.class, resources.getMessages()); 
} 

public CountryListEnumEncoder getCountryListEnumEncoder(){ 
     return new CountryListEnumEncoder(); 
} 

在我的模板(select.selectcountries延伸掛毯選擇組件BTW):

 <t:select.selectcountries id="country" t:id="country" model="CountryListEnum" value="user.address.countrycode" encoder="CountryListEnumEncoder"/> 

我枚舉:

public enum CountryListEnum { 

    AFGHANISTAN("Afghanistan", "AF"), 
    ALBANIA("Albania", "AL"), 
    ALGERIA("Algeria", "DZ"), 
    (...ETC); 

    private String country; 
    private String code; 

    private CountryListEnum(String country, String code) { 
     this.country = country; 
     this.code = code; 
    } 

    public String getCountry() { 
     return country; 
    } 

    public void setCountry(String country) { 
     this.country = country; 
    } 

    public String getId() { 
     return getCode(); 
    } 

    public String getCode() { 
     return code; 
    } 

    public void setCode(String code) { 
     this.code = code; 
    } 

    private CountryListEnum() { 
    } 

    public static int getSize() { 
     return values().length; 
    } 

    public static String getNameFromCode(String code) { 

     for (CountryListEnum countryEnum : values()) { 
      if (code.trim().equals(countryEnum.getCode())) { 
       return countryEnum.getCountry(); 
      } 
     } 
     throw new IllegalArgumentException("Country Code: "+ code + " does not exist"); 
    } 

} 

我的ValueEncoder:

public class CountryListEnumEncoder implements ValueEncoder<CountryListEnum>, ValueEncoderFactory<CountryListEnum> { 

    @Override 
    public String toClient(CountryListEnum value) { 
     return value.getId(); 
    } 

    @Override 
    public CountryListEnum toValue(String clientValue) { 
     Validate.notEmpty(clientValue); 

     for (CountryListEnum countryEnum : CountryListEnum.values()) { 
      if (clientValue.trim().equals(countryEnum.getCode())) { 
       return countryEnum; 
      } 
     } 

     throw new IllegalArgumentException("Country Code: " + clientValue + " does not exist"); 
    } 

    @Override 
    public ValueEncoder<CountryListEnum> create(Class<CountryListEnum> type) { 
     return this; //To change body of implemented methods use File | Settings | File Templates. 
    } 
} 

Finaly,我收到以下錯誤:

java.lang.String cannot be cast to com.starpoint.instihire.api.domain.reference.CountryListEnum

我試着犯類型脅迫者(如建議here),但也不能工作。我還嘗試省略select中的模型參數,並將select.selectcountries組件的id更改爲countryListEnum(如建議的here)。抓我的頭在這一個...

回答

1

看起來像user.address.countrycode是一個字符串,而不是一個CountryListEnum