0

在我的页面类中:

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

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

在我的模板中(select.selectcountries 扩展 Tapestry 选择组件顺便说一句):

      <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");
    }

}

我的价值编码器:

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.
    }
}

最后,我收到以下错误:

java.lang.String 不能转换为 com.starpoint.instihire.api.domain.reference.CountryListEnum

我尝试提交类型强制器(如此处所建议,但这也不起作用。我还尝试从 select 中省略模型参数,并将 select.selectcountries 组件的 id 更改为 countryListEnum (如此处建议。在这个问题上抓挠我的头......

4

1 回答 1

1

看起来user.address.countrycode是一个字符串而不是一个CountryListEnum

于 2013-08-21T07:35:26.820 回答