2013-08-19 100 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