PDA

View Full Version : Error in binding collections


sherihan
Feb 2nd, 2005, 03:30 AM
Hi,
I have a form that is used to edit invoice details.
It contains a button. Whenever the user clicks this button a new row is added (using javascript) so that the user can add a new item line to the invoice.

The invoice class is as follows:
public class Invoice extends BaseObject implements Serializable {

private Long id;
private Integer invoiceNumber;
private Customer customer;
private Date date;
private Set itemLines = new HashSet();
private double totalPrice;

The itemLine class is as follows:
public class ItemLine extends BaseObject implements Serializable {

private Long id;
private Item item;
private double quantity;
private double unitPrice;
private double totalPrice;
private Invoice invoice;


The item class is as follows:
public class Item extends BaseObject implements Serializable {

private Long id;
private String name;
private String code;
private double unitPrice;
private ItemType itemType;
private Set itemLines = new HashSet();

The JSP code is:
<spring:bind path="invoice.itemLines">
<c:forEach varStatus="loop" items="${invoice.itemLines}" var="anItemLine">
<tr>
<spring:bind path="invoice.itemLines[${loop.index}]">
<input type="hidden" name="${status.expression}" value="${anItemLine.id}"/>
<spring:bind path="invoice.itemLines[${loop.index}].item">
<td><input type="text" name="${status.expression}" value="${anItemLine.item.code}"/></td>
</spring:bind>
<spring:bind path="invoice.itemLines[${loop.index}].quantity">
<td><input type="text" name="${status.expression}" value="${status.value}"/></td>
</spring:bind>
</spring:bind>
<td><input type="button" name="deletebutton" value="Delete" onclick="javascript: deleteOldRow(this);"/></td>
</tr><% x++ ;%>
</c:forEach>
</spring:bind>

I created two propperty editors one for the itemLines and the other for the item.

I have no problem in showing an invoice.
All the invoice data and its item Lines are displayed correctly.
But the problem is that when I add a new item Line and try to submit the form, I get the following error:

org.springframework.beans.InvalidPropertyException :Invalid property 'itemLines[0]' of bean class [com._4s_.invoice.model.Invoice]: Property referenced in indexed property path 'itemLines[0]' is neither an array nor a List nor a Map; returned value was [3]

Why does this happen?
Could anyone help me?

Thanks in Advance.

robh
Feb 2nd, 2005, 07:00 AM
You can't access a Set using an index. You need to change the type of itemLines to an array, a List or a Map. Alternatively, create a new accessor to get the itemLines as an array for use in rendering.

Rob

sherihan
Feb 2nd, 2005, 08:33 AM
Hi Rob,
Thanks.

jeoffw
Jul 12th, 2006, 05:52 PM
You can't access a Set using an index. You need to change the type of itemLines to an array, a List or a Map. Alternatively, create a new accessor to get the itemLines as an array for use in rendering.


What if the spring binder were to iterate through the Set the specified number of times? For a given instance the iteration order should be predictable, right?

void getIndexOfSet(int index, Set set) {
Object result = null;
Iterator it = set.iterator();
for(int i=0; i <= index && it.hasNext(); i++) {
result = it.next();
}


That way client-side forms could reference elements of a set by index without needing to add special accessor methods. In fact, that method would work for binding to any java Collection.