PDA

View Full Version : <spring: bind> help


onlyankur
09-22-2005, 06:01 PM
My application has a lot of pages binded together in the spring framework. i populated some drop-down-boxes on some of these pages from the DB. Now i want to get what the user selected in the comboboxes or radio boxes and accordingly give the user some more info from the database. i dont know how to get what the user selected in spring. i tried java script but thru that i cannot send what the user selected in a request or session variable but it didnt work.
eg. request.setAttribute("value1",document.forms[0]._dropdownbox1.value) will not work.

This is some piece of code from my jsp
<spring:bind path="GroupListITBean.ITGroupList">
<select name='ITGroupList'>
<c:forEach items="${GroupListITBean.ITGroupList}" var="groupList">
<option value='<c:out value="${groupList}"/>'><c:out value="${groupList}"/></option>
/>
</c:forEach>
</select>

GroupListBean is the command name give in the deployment descriptor to my command class.
ITGroupList calls the getITGroupList() in the command class.
The getITGroupList() gets data from the database.

I would really appreciate if u could give me some info on this one.

Rick Evans
09-22-2005, 09:54 PM
Hi

You need to post your issue here (http://forum.springframework.org/viewforum.php?f=3&sid=11ae2d64e104040f9fcbe6aef4822da9)

This is the forum for Spring.NET, the .NET port of the Spring Framework for Java. We really must change the logo that is displayed on the top left of this forum :?

Now that I'm here though... why not simply bind the value of the <select/> tag to a property on your command class (form backing object)? For example, you could have a property on your command class called 'indexofTheSelectedITGroup', and bind that in the <spring:bind/> tag. A little like this...

public class GroupListITBean {

private List iTGroupList;

private int indexofTheSelectedITGroup;

// getters and setters for the above fields omitted...

public Group getSelectedGroup() {
return this.iTGroupList.get(this.indexofTheSelectedITGrou p);
}
}

With the attendant JSP fragment...

<spring:bind path="GroupListITBean.indexofTheSelectedITGroup">
<c:set var="index" value="0" scope="page"/>
<select name="${status.expression}">
<c:forEach items="${GroupListITBean.ITGroupList}" var="groupList">
<c:choose>
<c:when test="${index == status.value}">
<option value="<c:out value="${index}"/>" selected="true">
<c:out value="${groupList}"/>
</option>
</c:when>
<c:otherwise>
<option value="<c:out value="${index}"/>">
<c:out value="${groupList}"/>
</option>
</c:otherwise>
</c:choose>
<c:set var="index" value="${index + 1}"/>
</c:forEach>
</select>
</spring:bind>

And you could access the selected value in your (SimpleForm) Controller like so...

protected ModelAndView onSubmit(final Object command) {
GroupListITBean bean = (GroupListITBean) command;
Group selectedGroup = bean.getSelectedGroup();
// rest of method omitted for clarity...
}

Mmm, thats a bit convoluted ain't it? It also means that you are introducing a synthetic property into your business object class to satisfy the needs of the web tier. You could also register a custom PropertyEditor in the initBinder method of your Controller, and have Spring convert the user's selected value (a string) into a full-on Group instance. This would obviate the need for any synthetic property shenanigans.

Ah, I see that you have discovered the correct forum (http://forum.springframework.org/viewtopic.php?t=9080). Those real Spring folks will probably give you a better answer for your money :wink:

Ciao
Rick