wood burning stoves 2.0*
The moose likes JSP and the fly likes cannot iterate over same Result more than once Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "cannot iterate over same Result more than once" Watch "cannot iterate over same Result more than once" New topic
Author

cannot iterate over same Result more than once

Ken Hartz
Greenhorn

Joined: Jul 08, 2006
Posts: 14
I'm using a nested <c:foreach loop, I'm creating a ResultSet in a standard Java class, utilizing ResultSet.TYPE_SCROLL_INSENSITIVE etc... with createStatement, and I'm using ResultSuport to convert the ResultSet to a Result object. Also, in my <c:foreach I'm specifying begin="0".

In controller servlet I populate a requestAttribute with this returned Result object.

Problem: I cannot iterate over the same result list more than once.

I'm using webLogic 9.0 with the Oracle thin driver.

Any ideas why I cannot iterate over the same result list more than once in my page?

Thanks.
[ September 01, 2006: Message edited by: Bear Bibeault ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60800
    
  65

Originally posted by Ken Hartz:
Problem: I cannot iterate over the same result list more than once.


This doesn't tell us very much about what you are experiencing.
[ September 01, 2006: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60800
    
  65

Also you stated:

In controller servlet I populate a requestAttribute with this returned Result object.


Result isn't an iterable class, so you'll need to spend a little more time on your posts and be more explicit about what you are doing. Otherwise, you are just wasting everybody's time including your own.

Also, "ResultSuport" is really ResultSupport. Please take care to avoid such carelessness in your posts.
Ken Hartz
Greenhorn

Joined: Jul 08, 2006
Posts: 14
Let me provide my code below, maybe that will help (sorry for wasting the sheriff's time). Note the below works except that the inner loop only displays the members list (column for each task listed in outer loop) for the first task, the remaining tasks (outer loop) show a blank select list for members from then on.

snippet from class that gets data:
----------------------------------
stmt = myConn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);

query="select nbid, first_name from tbl_amis_members order by last_name";
rs = stmt.executeQuery(query);
rslt = ResultSupport.toResult(rs);
rs.close();
stmt.close();

return rslt;

snippet from the servlet that acts as traffic controller:
---------------------------------------------------------
request.setAttribute("amisMembers", amembers.findMembers());
address = "[jsp-file-listed-below]";
RequestDispatcher dispatcher = request.getRequestDispatcher(address);
dispatcher.forward(request, response);

snippet from JSP page where results are displayed:
---------------------------------------------------
<c:forEach var="amisTasks" begin="0" items="${amisTasks.rows}" varStatus="counter">
<tr>
<form name="amis_tasks_update" method="post" action="/amis/AmisController">
<td><cut value="${amisTasks.id}"/>
<input type="submit" value="update" />
<td><cut value="${amisTasks.description}"/></td>
<select name="task_owner" >
<option value="${amisTasks.nbid}">
<cut value="${amisTasks.first_name}"/></option>
HERE BELOW is the inner loop that only gets displayed the first time around
<c:forEach var="amisMembers" begin="0" items="${amisMembers.rows}" varStatus="counter2">
<option value="${amisMembers.nbid}">
<cut value="${amisMembers.first_name}"/></option>
</c:forEach>
</select>
</td>
</form>
</tr>
</c:forEach>

Thanks for any help anyone can provide.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60800
    
  65

One more thing, please be sure to use UBB code tags when posting code. That will preserve any formatting and make your code easier to read. See here.

Remember that the row property of Result returns an array of Map instances. Therefore when you iterate over the items in a particular row, it will be an instance of Map.Entry.

Here's a stripped down example of iterating over a Result (assuming it's in scope as result):

Ken Hartz
Greenhorn

Joined: Jul 08, 2006
Posts: 14
I'll try it out, thanks!
Ken Hartz
Greenhorn

Joined: Jul 08, 2006
Posts: 14
The code example provided did not work for me, but maybe it's because I did not explain well enough.

In order to simply display something like the below twice in the same page in 2 separate <c:foreach loops on 2 separate areas of the page, what needs to be done? Is this a scope issue, loop.index issue?

The below code does not display the list in the second <c:foreach loop.
[code]
<c:forEach var="amisMembersAsc" begin="0" items="${amisMembersAsc.rows}" varStatus="counter">
<p><cut value="${amisMembersAsc.first_name}"/></p>
</c:forEach>
<c:forEach var="amisMembersAsc" begin="0" items="${amisMembersAsc.rows}" varStatus="counter">
<p><cut value="${amisMembersAsc.nbid}"/></p>
</c:forEach>
</code>

Thanks again for any help you can provide, this issue is getting frustrating.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60800
    
  65

If the code example did not work once, it's certainly not going to work twice. There is no reason that a Result placed in scope should not work with the code I posted. So the first step is to get that to work.

So, what does "didn't work" mean?
Ken Hartz
Greenhorn

Joined: Jul 08, 2006
Posts: 14
The code I just posted today does work for that first <c:foreach loop.
What does not work is the second time it loops through that Result, it doesn't display anything.
Your example did not seem to be what I was looking for because I'm dealing with 2 separate db tables.
I provided today's code example to simplify things (to get off of the nested loop issue), because I simply cannot display the same list twice in one page which I believe is the same issue.
Can you confirm, that with one list (from Result etc...), that you are able to use 2 separate <c:foreach loops on 2 separate areas of the page and display that same list twice?
If so can you please provide the code in your confirmed example? I'm looking for a list from a returned Result object etc... that displays column data.

Thanks.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60800
    
  65

Originally posted by Ken Hartz:
If so can you please provide the code in your confirmed example? I'm looking for a list from a returned Result object etc... that displays column data.


I did. That's exactly what the code I posted was. A Result instance was place in request scope by a servlet controller and the posted code displayed the table contents perfectly.

Cutting and pasting the same code multiple times on the page resulting in multiple dumps of the table. No problems.

If you can't get a simple page running with that code with your Result, perhaps you're not setting what you think you're setting on the page.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60800
    
  65

<c:forEach var="amisMembersAsc" begin="0" items="${amisMembersAsc.rows}" varStatus="counter">


Drop the begin="0" clause; it's superfluous.
[ September 02, 2006: Message edited by: Bear Bibeault ]
Ken Hartz
Greenhorn

Joined: Jul 08, 2006
Posts: 14
I dropped the begin="0" and with the below simplified code, I still only get one list displayed when I should get 2 copies of the list displayed.

Here's the code I'm using:


Maybe if you can confirm that you can get 2 lists displayed with the above code on your end will shed light?

The default scope should be page so it doesn't seem like I need set any scope for the var.

Any ideas, this issue is a deal breaker in my application.

Thanks for trying to help me resolve this issue.
Ken Hartz
Greenhorn

Joined: Jul 08, 2006
Posts: 14
Note, I'm using 2 different columns in both of my lists, but I still don't get the second list.

Thanks again.
Ken Hartz
Greenhorn

Joined: Jul 08, 2006
Posts: 14
Finally did get this to work and you were correct Bear, you can iterate over a same list as many times as you want.

For whatever reason, renaming the "var" name in the <c:foreach did the trick. I thought I had done this before so it's a bit confusing, but I'm relieved that it's working.

Thanks for spending the time helping.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: cannot iterate over same Result more than once
 
Similar Threads
For Each ArrayList of Object in JSTL
Use of deferred expressed in iteration doesn't work
bean and for each
example of displaying same collection twice on same page?
How to iterate ResultSet multiple times