public class CancerTypePropertyEditor extends PropertyEditorSupport {
private CancerTypeDAO dao;
public CancerTypePropertyEditor(CancerTypeDAO dao, Class collectionType){
super(collectionType);
this.dao = dao;
}
@Override
public void setAsText(String text) throws IllegalArgumentException {
Object obj = getValue();
List list = (List)obj;
for(String str : text.split(",")){
list.add(dao.retrieveCancerType(Long.valueOf(str)));
}
}
@Override
public String getAsText() {
// TODO Auto-generated method stub
return super.getAsText();
}
}
In Controller:
@Override
protected void initBinder(HttpServletRequest request,
ServletRequestDataBinder binder) throws Exception {
super.initBinder(request, binder);
binder.registerCustomEditor(List.class, "cancerTypes",
new CancerTypePropertyEditor(getCancerTypeDao(),
List.class));// new CancerTypePropertyEditor ());
}
In CancerType POJO (ignore the confusing variable naming):
@Override
public boolean equals(Object anObject) {
if (anObject == null) {
return false;
} else if (this == anObject) {
return true;
} else if (anObject instanceof CancerType) {
final CancerType aCountry = (CancerType) anObject;
Long aCountryId = aCountry.getId();
if (aCountryId != null) {
return aCountry.getId().equals(id);
}
}
return false;
}
The tag:
<form:select multiple="true"
id="cancer-type" path="cancerTypes" items="${cancerTypeLookups}" itemLabel="description" itemValue="id" cssStyle="width:500">
</form:select>
|