Reputation: 781
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'
Upvotes: 8
Views: 11361
Reputation: 33785
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());
Upvotes: 4
Reputation: 242696
Perhaps, the type
property of the command object is decalred as Enum
instead of TestType
?
Upvotes: 0