PDA

View Full Version : Multiple Select with Spring MVC


mikeanderson
Jul 16th, 2007, 12:35 PM
Anyone has an example of multiple select using spring binding. please help me.

Thanks,
Mike

TerpInMD
Jul 16th, 2007, 01:24 PM
I have done it.

Thanks,

mikeanderson
Jul 16th, 2007, 01:50 PM
could you provide some code of your contoller class and jsp page.

mikeanderson
Jul 16th, 2007, 02:10 PM
Hi ,

I am new to the spring frame works. I don't know how to do it multiple select using spring binding. Do you have any example code or any link please let me know.

Thanks in Advance,
Mike.

TerpInMD
Jul 16th, 2007, 02:15 PM
Here are the imporant snippets:


<form:select path="fwUser.groups" multiple="true" size="10" cssStyle="width: 275px" >
<form:options items="${command.fwUser.groups}" itemLabel="name" itemValue="id" />
</form:select>



protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception
{
binder.registerCustomEditor(Set.class, "fwUser.groups", new CustomCollectionEditor(Set.class)
{
protected Object convertElement(Object element)
{
if (element != null) {
Long id = new Long((String)element);
Group group = getFwService().findGroupById(id);
return group;
}
return null;
}
});
}

mikeanderson
Jul 16th, 2007, 03:23 PM
thanks for sending code.

TerpInMD
Jul 16th, 2007, 04:49 PM
Let me know if you can get it working based on my example. It took me a few days to figure out that little bit of code.

mikeanderson
Jul 16th, 2007, 06:28 PM
Thanks you TerpInMD for giving quick response. Actually i have fixed this problem for using spring binding. Here is the sample code

Jsp
-----

<spring:bind path="submitForm.division">
<select name="<core:out value="${status.expression}"/>">
<core:forEach items="${divisionMap}" var="divisionCode">
<option
<core:if test="${status.value == divisionCode}">selected</core:if>
value="<core:out value="${divisionCode}"/>">
<core:out value="${divisionCode}"/></option>
</core:forEach>
</select>
</spring:bind>





Controller
----------


protected Map referenceData(HttpServletRequest arg0) throws Exception {
// TODO Auto-generated method stub
List divisionList = new ArrayList();
divisionList.add("1st Id");
divisionList.add("2nd Id");
divisionList.add("3rd Id");
divisionList.add("4th Id");
divisionList.add("5th Id");
divisionList.add("6th Id");
Map referenceData = new HashMap();
referenceData.put("divisionMap", divisionList);
return referenceData;
}



Thanks,
Mike.