Java Spring表单复选框-通过toString()匹配,从而勾选错误的复选框
我有一个域对象ContactGroup,其中包含对另一个域对象Person的大量引用:Java Spring表单复选框-通过toString()匹配,从而勾选错误的复选框,java,forms,spring-mvc,checkbox,Java,Forms,Spring Mvc,Checkbox,我有一个域对象ContactGroup,其中包含对另一个域对象Person的大量引用: @Entity public class ContactGroup { @ManyToMany private Set<Person> members = new HashSet<>(); [...] } <form:checkboxes path="members" multiple="true" items="${personList}" itemLabel="d
@Entity
public class ContactGroup {
@ManyToMany
private Set<Person> members = new HashSet<>();
[...]
}
<form:checkboxes path="members" multiple="true" items="${personList}" itemLabel="displayString" itemValue="id"/>
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.setValidator(validator);
binder.registerCustomEditor(Person.class, new PropertyEditorSupport() {
@Override
public void setAsText(String text) throws IllegalArgumentException {
setValue(personService.get(Long.parseLong(text)));
}
});
binder.registerCustomEditor(Set.class, "members", new CustomCollectionEditor(Set.class) {
@Override
protected Object convertElement(Object element) {
if (element instanceof Long) {
return personService.get((Long) element);
} else if (element instanceof String) {
personService.get(Long.parseLong((String) element));
} else if (element instanceof Person) {
return element;
}
return null;
}
});
}