类型是对象中的枚举属性。
jsp:
<form:radiobutton path="type" value="Male" />
Java:
public enum TestType
{
Male, Female;
}
出现错误
无法将值“Male”从类型“java.lang.String”转换为类型“java.lang.Enum”; reason = 'java.lang.Enum 不是枚举类型'
最佳答案
如下操作
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;
}
}
并按如下方式注册自定义 Binder
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();
}
});
然后
<form:radiobuttons path="type" items="${testTypeList}" itemLabel="description"/>
您按如下方式设置您的 TestType
model.addAttribute(TestType.values());
关于java - 如何将 ENUM 绑定(bind)到单选按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3478177/