PDA

View Full Version : Nested binding


mangvlad
Oct 6th, 2005, 08:14 PM
I am trying to bind users input to an object that has an array of objects, where each element is an array itself.

It appears that it completely ignores my binding (no errors, it just not setting values).

Has anyone seen the same problem?


So, my object has an array of roles. Each role has an array of resources.
Both Role object and Resource have a field called selected. The display part works ok: it displays (binds) all the right values, but after it submitted, only first level elements are populated (only roles that were checked have selected set to true, all resources always have selected set to false (default) and not what user clicks).

Sample code:

User object contains: private Role[] roles;
Role object contains: private boolean selected; private String description; private Resource[] resources;
Resource object contains: private boolean selected; private String description;


<%
for (int i=0; i< user.getRoles().length; i++) {
String selectedName = "user.roles["+i+"].selected";
%>
<tr>
<td align="center">
<spring:bind path="<%=selectedName%>">
<input type="hidden" name="_<c:out value="${status.expression}"/>">
<input type="checkbox"
name="<c:out value="${status.expression}"/>"
value="true"
<c:if test="${status.value}">checked</c:if>
/>
</spring:bind>
</td>
<td> <%=user.getRoles()[i].getDescription()%>
</td>
</tr>
<table align="center">
<%
for (int j=0; j< user.getRoles()[i].getResources().length; j++) {
String selectedRes = "user.roles["+i+"].resources["+j+"].selected";
%>
<tr>
<td align="center">
<spring:bind path="<%=selectedRes%>">
<input type="hidden" name="_<c:out value="${status.expression}"/>">
<input type="checkbox"
name="<c:out value="${status.expression}"/>"
value="true"
<c:if test="${status.value}">checked</c:if>
/>
</spring:bind>
</td>
<td>
<%=user.getRoles()[i].getResources()[j].getDescript ion()%>
</td>
</tr>
<%
}
%>
</table>
<%
}
%>



Thanks.

Tuomaz
Oct 7th, 2005, 02:25 AM
I had a problem like this once before. After some debugging I found out that I had forgot to update my call to binder.setAllowedFields(), the result was that some of the fields in my form always ended up with their default value simply because I didn't allow the fields to be posted :-)


protected void initBinder(HttpServletRequest request,
ServletRequestDataBinder binder)
throws Exception
binder.setAllowedFields(new String[] {...all your fields here...});

}


now your problem probably doesn't have anything to do with this (maybe you doesn't even call setAllowedFields()), but maybe maybe...