Question about Struts <logic:iterate> tag

Discussion in 'Java' started by javadude, Jun 30, 2005.

  1. javadude

    javadude Guest

    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.
    javadude, Jun 30, 2005
    #1
    1. Advertising

  2. javadude

    Scott Phelps Guest

    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"/>


    "javadude" <> wrote in message
    news:...
    >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.
    >
    Scott Phelps, Jun 30, 2005
    #2
    1. Advertising

  3. javadude

    Wendy Smoak Guest

    "javadude" <> wrote:

    > 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.)

    --
    Wendy Smoak
    Wendy Smoak, Jun 30, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gogo
    Replies:
    1
    Views:
    2,083
    Sudsy
    Sep 4, 2003
  2. Jack_A

    Struts tag <logic:iterate>

    Jack_A, May 18, 2004, in forum: Java
    Replies:
    1
    Views:
    8,772
    Martin
    May 19, 2004
  3. Replies:
    3
    Views:
    10,918
    yehuda54
    Feb 28, 2008
  4. runescience
    Replies:
    0
    Views:
    1,439
    runescience
    Feb 9, 2006
  5. Replies:
    3
    Views:
    3,615
Loading...

Share This Page