views:

144

answers:

2

Type is enum property in object.

jsp:

<form:radiobutton path="type" value="Male" />

java:

public enum TestType
{
    Male, Female;
}

and got error

Unable to convert value 'Male' from type 'java.lang.String' to type 'java.lang.Enum'; reason = 'java.lang.Enum is not an enum type'

A: 

Perhaps, the type property of the command object is decalred as Enum instead of TestType?

axtavt
A: 

Do as follows

public enum TestType {

    MAN("Man"),
    FEMALE("Female");

    private String description;

    private TestType(String description) {
        this.description = description;
    }

    public String getValue() {
        return name();
    }

    public void setValue(String value) {}

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

}

And register a custom binder as follows

dataBinder.registerCustomEditor(TestType.class, new PropertyEditorSupport() {
        @Override
        public void setAsText(String value) throws IllegalArgumentException {
            if(StringUtils.isBlank(value))
                return;

            setValue(TestType.valueOf(value));
        }

        @Override
        public String getAsText() {
            if(getValue() == null)
                return "";

            return ((TestType) getValue()).name();
        }
    });

Then

<form:radiobuttons path="type" items="${testTypeList}" itemLabel="description"/>

You set up your TestType as follows

 model.addAttribute(TestType.values());
Arthur Ronald F D Garcia