[英]How to bind ENUM to radiobutton?
Type is enum property in object. Type是对象中的枚举属性。
jsp: JSP:
<form:radiobutton path="type" value="Male" />
java: Java的:
public enum TestType
{
Male, Female;
}
and got error 并得到错误
Unable to convert value 'Male' from type 'java.lang.String' to type 'java.lang.Enum'; 无法将'male'从'java.lang.String'类型转换为'java.lang.Enum'类型; reason = 'java.lang.Enum is not an enum type' reason ='java.lang.Enum不是枚举类型'
一个更简单的解决方案可以在spring论坛上找到,无需任何自定义绑定。
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 您可以按如下方式设置TestType
model.addAttribute(TestType.values());
也许,命令对象的type
属性是否为Enum
而不是TestType
?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.