aspose file tools*
The moose likes Spring and the fly likes Binding with ArrayList in Spring Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Binding with ArrayList in Spring" Watch "Binding with ArrayList in Spring" New topic
Author

Binding with ArrayList in Spring

pasquale vitale
Greenhorn

Joined: Aug 22, 2008
Posts: 10
Hi all, I'm new with spring framework.

I have a bean AddExameForm with this parameters:

private String esame;
private String descrizione;
private List<Domanda> domande;

and the costructor is:
//initilize ArrayList with objects domanda
domande = new ArrayList<Domanda>(10);
for (int i=0;i<10;i++){
domande.add(new Domanda());
}

... setter and getter methods;

and bean Domanda is a bean with:

private String domanda;
... setter and getter

Now I want to populated my bean AddExameForm (with domande) and I using a binding; in the other words I want a form with 10 input (type=text) where I want to write my 10 strings (text) and then when I push a submit button I want to initialize my bean (my List domande) with my 10 strings.

My jsp code is:

<c:forEach varStatus="i" var="domande" items="${addExameForm.domande}">
<tr>
<td class="blank"><cut value="${i.count}" /></td>
<td class="blank">
<spring:bind path="addExameForm.domande[${i.index}].domanda">
<input type="text" name="<cut value="${status.expression}"/>" value="<cut value="${status.value}"/>">
<font color="red"><cut value="${status.errorMessage}" /></font>
</spring:bind>
</td>
</tr>
</c:forEach>

when I submit form I have this error:

javax.servlet.ServletException: org.springframework.beans.InvalidPropertyException : Invalid property 'domande[0]' of bean class [springweb.form.admin.AddExameForm]: Index of out of bounds in property path 'domande[0]'; nested exception is java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

Can you help me?
Thanks Pasquy
Vyas Sanzgiri
Ranch Hand

Joined: Jun 16, 2007
Posts: 686

Sorry but I am trying to revive an old thread. This is important for me. I am still not able to figure this out


===Vyas Sanzgiri===
My Blog
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
It should work. Have you tried debug? Does the constructor get called?

Anyway, next time you should post in Spring forum (in this website).


SCJA 1.0, SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJP 5.0, SCEA 5, SCBCD 5; OCUP - Fundamental, Intermediate and Advanced; IBM Certified Solution Designer - OOAD, vUML 2; SpringSource Certified Spring Professional
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Yes, I would add debug to the constructor, *and* to the getter and setter - there may be something in the flow of the logic generating the page that looks for an existing "domande" value and if one isn't found sets it to an empty ArrayList.


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Vyas Sanzgiri
Ranch Hand

Joined: Jun 16, 2007
Posts: 686

I believe this is a known issue in Spring but I cannot find the work around
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
Vyas Sanzgiri wrote:I believe this is a known issue in Spring but I cannot find the work around

I don't think so. If it's a bug, it needs to be fixed. It's very normal to access an item in list.

Anyway, have you debugged?
Vyas Sanzgiri
Ranch Hand

Joined: Jun 16, 2007
Posts: 686

Try it. Its not a bug it is an issue which has some work around. It is very difficult for me to get into the details of Spring as I have not debugged the framework.

Spring spits of all sorts of DEBUG level messages
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
I have no problem binding to items in list. What is your Spring version?
If Spring cannot do that, it's a bug for sure.
Vyas Sanzgiri
Ranch Hand

Joined: Jun 16, 2007
Posts: 686

Spring MVC 2.5. Can you share your code?
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
I use SimpleFormControler, create empty items and add to list in formBackingObject method.
In JSP I use something like:

There is no problem.

It's a very basic feature, Spring must be able to do this.
Vyas Sanzgiri
Ranch Hand

Joined: Jun 16, 2007
Posts: 686

I am using annotations.

In my form I had to declare List as follows:



Otherwise it gives me InvalidPropertyException IndexOutOfBoundsException
 
jQuery in Action, 2nd edition
 
subject: Binding with ArrayList in Spring