bind problems: list, select
Hi,
I am having difficulty getting spring:bind to work on a list.
My object graph is a little complicated, but if I can get this to work, I can use it over many pages, so I think it is worth the effort.
Here are the relevant objects:
Code:
public class ProfileInfo extends AbstractInfo {
List populatedSelects;
.... including getters/setters...
}
Code:
public class PopulatedSelect {
private Select select = new Select();
private AttributeRating attributeRating = new AttributeRating();
.... including getters/setters...
Code:
public class Select {
private String name;
private String title;
private String category;
private List options = new ArrayList();
private int selected;
.... including getters/setters...
Code:
public class AttributeRating {
private String userName;
private String attributeName;
private int rating = 0;
.... including getters/setters...
features.jsp:
Code:
<TABLE>
<c:forEach items="${profileInfo.populatedSelects}" var="populatedSelect" varStatus="varStatus">
<TR><td><c:out value="${populatedSelect.select.title}"/></td>
<TD>
<spring:bind path="profileInfo.populatedSelects[${varStatus.index}].attributeRating.rating">
<select name="<c:out value="${populatedSelect.select.name}"/>" size="1">
<c:forEach items="${populatedSelect.select.options}" var="option">
<option <c:if test="${populatedSelect.attributeRating.rating == option.index}">selected</c:if> value="<c:out value="${option.index}"/>"><c:out value="${option.value}"/></option>
</c:forEach>
</select>
</spring:bind>
</TD>
</spring:bind>
</TR>
</c:forEach>
</TABLE>
I have had to use a Select object, because I have many pages using a variable number of selects, and I needed to keep all these properties in an XML file. I need the AttributeRating object because later I want to make it so that is a user doesn't like any of the options offered in a select, he can fill in his own value into a textfield - but I have removed this code for now.
It all works in the display, except that the value selected in the select options doesn't get bound to the field AttributeRating.rating.
Adding the line:
Code:
rating test = <c:out value="${profileInfo.populatedSelects[varStatus.index].attributeRating.rating}"/>
after the spring:bind tag outputs the correct value for attributeRating.rating for each tag.
I guess I have something wrong in the spring:bind tag - can anyone help?
Many thanks,
John Pedersen
Missing the status in your bind
Code:
<spring:bind path="profileInfo.populatedSelects[${varStatus.index}].attributeRating.rating">
<select name="<c:out value="${populatedSelect.select.name}"/>" size="1">
<c:forEach items="${populatedSelect.select.options}" var="option">
<option <c:if test="${populatedSelect.attributeRating.rating == option.index}">selected</c:if> value="<c:out value="${option.index}"/>"><c:out value="${option.value}"/></option>
</c:forEach>
</select>
</spring:bind>
Maybe I've gone bleery eyed but I can't see a ${status} reference in your code. Am I missing it?
Also you have a 2nd "</spring:bind>" after all of that. Is that supposed to close off one not listed in your code?
I would also look into upgrading to the "${option.value}" syntax versus the <c:out/> one. Easier on the eyes. Also I would see if writting a custom tag would help you out, especially if you are going to use this code in other pages.