File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Trying to iterate an ArrayList of Beans Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Trying to iterate an ArrayList of Beans" Watch "Trying to iterate an ArrayList of Beans" New topic
Author

Trying to iterate an ArrayList of Beans

Bryce Martin
Ranch Hand

Joined: Nov 19, 2007
Posts: 269
I have an ArrayList in which each element of the ArrayList is an instance of my Bean. I'm trying to use the <c:forEach> tag to iterate through it in my jsp but I get this error...

[11/29/07 16:09:31:794 EST] 0000002f SystemErr R javax.servlet.ServletException: The "." operator was supplied with an index value of type "java.lang.String" to be applied to a List or array, but that value cannot be converted to an integer.

Here is what my code looks like...



my list is called "list". am I missing something with the naming conventions? orderNumber is the only unique column in the arraylist... am I not referencing it right? I'm kind of confused...but i thought this would work...
[ November 30, 2007: Message edited by: Bear Bibeault ]
Bryce Martin
Ranch Hand

Joined: Nov 19, 2007
Posts: 269
I figured it out. I needed to change the items="${list.orderNumber}" to items="${row.orderNumber}". Now I get no errors. And I get no values printed either. I need to check if it is if my beans are empty or if my EL isn't working...
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61201
    
  66

<c:forEach var="list" items="${list.orderNumber}">
If you want to iterate over the list, why are you specifying a property of the list in the items attribute?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bryce Martin
Ranch Hand

Joined: Nov 19, 2007
Posts: 269
yeah, i realized that and changed it to just be items="${row}" but that still hasn't produced results... I'm then using the EL like this...

${row.orderNumber}

I'm just not positive that will give me what I want, but I think it should.
Bryce Martin
Ranch Hand

Joined: Nov 19, 2007
Posts: 269
How would I just dump the contents of my arraylist to the page? I know I would have to iterate over it, but is there a way to dump all the data in the rows without calling each element of the bean?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61201
    
  66

Where does "row" come from. You stated the that scopde variable for the list was named "list".
Bryce Martin
Ranch Hand

Joined: Nov 19, 2007
Posts: 269
Hey Bear, sorry about the incomplete information. My bean is called row. I do


Then I do


^^^^^I do this for each element I will need in my jsp.

Then I do list.add(row);

and finally I request.setAttribute("list",list);


Should I not be using "row" anywhere in my <c:forEach>?
This is how I have it now...

Bryce Martin
Ranch Hand

Joined: Nov 19, 2007
Posts: 269
WHEEEEE, I got it working. Only one small problem to overcome now. I need to clear the arraylist. If I run it, and then close my browser, an then come back my server is remembering the old arraylist. How do I go about making sure its empty?

I'm gonna check the methods for arraylist and see if that have a way to wipe it clean...i'm sure they do...

thanks guys
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61201
    
  66

and finally I request.setAttribute("list",list);
The name of the Java variable is moot. You've created a scoped variable named "list".
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61201
    
  66

Originally posted by Bryce Martin:
If I run it, and then close my browser, an then come back my server is remembering the old arraylist.
It can;t. You've assigned it to local variables and to a request scoped variable that goes out of scope when the response is returned to the browser. It;s much more likely that the page is being cached by the browser.
Bryce Martin
Ranch Hand

Joined: Nov 19, 2007
Posts: 269
How do I keep it from caching then? I can't have that.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61201
    
  66

See the Servlets FAQ.
Bryce Martin
Ranch Hand

Joined: Nov 19, 2007
Posts: 269
It didn't work. I don't think its a caching issue. I create the list every time I run the servlet. But if the List already exists in memory it won't recreate it right? The reason why I think this is because I restarted the server and the array list was clear and worked as it was supposed to.
[ November 30, 2007: Message edited by: Bryce Martin ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61201
    
  66

Originally posted by Bryce Martin:
I create the list every time I run the servlet. But if the List already exists in memory it won't recreate it right?
Of course it will. Java is going to do exactly what you tell it to do. If you tell it to create a new list, it's not going to search around for an existing one -- it'll create the new list.

Perhaps it's time to open a new topic in the Servlets forum for this issue showing your list creation code.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Trying to iterate an ArrayList of Beans