Question : Binding Select (Multiple)

Hi Experts ..

I am having troubles when trying to bind a list with select (multiple).

The class entity::
1:
2:
3:
4:
public class Regimen implements Serializable {
	private List<CancerType> cancerTypes;
}


The jsp:
1:
2:
3:
<form:select multiple="true" iid="cancer-type"  path="cancerTypes" items="${cancerTypes}" itemLabel="description" itemValue="id" cssStyle="width:500">							
</form:select>


I am getting the select box populated with all items in ${cancerTypes}, but the item listed in regimen.cancerTypes is not marked as *selected*, also when submitting the form I am getting
the following in BindException (errors):
1:
2:
3:
org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'regimen' on field 'cancerTypes': rejected value [[Ljava.lang.String;@4338fa]; codes [typeMismatch.regimen.cancerTypes,typeMismatch.cancerTypes,typeMismatch.java.util.List,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [regimen.cancerTypes,cancerTypes]; arguments []; default message [cancerTypes]]; default message [Failed to convert property value of type [java.lang.String[]] to required type [java.util.List] for property 'cancerTypes'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [java.lang.String] to required type [com.oncozoom.protocol.entities.CancerType] for property 'cancerTypes[0]': no matching editors or conversion strategy found]


Any idea how to fix this issue?
Thx!

Answer : Binding Select (Multiple)

I fixed it by extending PropertyEditorSupport  instead of CustomCollectionEditor, and I have also added an Equals to the POJO to compare IDs, and it works like a charm.

Thanks for your help,
Ahmad
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
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>
Random Solutions  
 
programming4us programming4us