• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Binding with ArrayList in Spring

 
pasquale vitale
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 686
Chrome Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry but I am trying to revive an old thread. This is important for me. I am still not able to figure this out
 
Hong Anderson
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It should work. Have you tried debug? Does the constructor get called?

Anyway, next time you should post in Spring forum (in this website).
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vyas Sanzgiri
Ranch Hand
Posts: 686
Chrome Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe this is a known issue in Spring but I cannot find the work around
 
Hong Anderson
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 686
Chrome Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1936
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 686
Chrome Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Spring MVC 2.5. Can you share your code?
 
Hong Anderson
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 686
Chrome Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using annotations.

In my form I had to declare List as follows:



Otherwise it gives me InvalidPropertyException IndexOutOfBoundsException
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic