Question about Struts <logic:iterate> tag

J

javadude

I have an action class that creates an ArrayList of Foo objects and
then sets them in the request. Note I am putting the list itself
directly into the request:

FooAction.java:
List fooList = new ArrayList();
for (int i = 0; i < 3; i++)
{
String id = "ID" + i;
Foo f = new Foo(id);
fooList.add(f);
}
request.setAttribute("foos", fooList);
// forward to JSP for display


In my JSP, I want to iterate over and display the Foo objects and
display their IDs.

I have:
<logic:iterate id="nextElement" name="foos">
<br>NextFoo<bean:write name="nextElement" property="id" /></b>
</logic:iterate>

However, I get the error:
Cannot find bean nextElement in any scope'

What am I doing wrong?
Thanks in advance.
 
S

Scott Phelps

The jsp has to know where to access the bean. If it is just in the page put
this at the top,
<jsp:useBean id="foos" scope="session" class="java.util.ArrayList"/>
 
W

Wendy Smoak

javadude said:
request.setAttribute("foos", fooList);
// forward to JSP for display

In my JSP, I want to iterate over and display the Foo objects and
display their IDs.

I have:
<logic:iterate id="nextElement" name="foos">
<br>NextFoo<bean:write name="nextElement" property="id" /></b>
</logic:iterate>

However, I get the error:
Cannot find bean nextElement in any scope'

You code (slightly modified) worked fine for me, so my guess is that
something's NQR with how you're filling the List or placing it in the
request. That or the code you posted isn't actually the code that's
producing the error.

Here's some code you can include at the bottom of your JSP...
http://wiki.wendysmoak.com/cgi-bin/wiki.pl?DebugJSP
It just prints out everything in the request/session/context... make sure
everything is where you think it is.

For starters, try just <bean:write name="foos" /> and make sure it can find
the list, and that the list has what you think it has in it. Hopefully your
'Foo' class has a good 'toString' method.

(You don't have to <jsp:usebean> with the Struts tags, it knows how to look
in the various scopes to find objects. Anyway, it's not complaining about
'foos', it's complaining about 'nextElement' inside the iteration.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,735
Latest member
HikmatRamazanov

Latest Threads

Top