• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 9089
  • Last Modified:

How to use c:forEach to retrieve properties from an ArrayList of JavaBeans

From a jsp page, I am using jsp:setProperty to set a String property in a parent bean.  The property value is then used as a parameter to a SQL query.  After the query runs, the parent bean instantiates a child bean for each row of the resultset and sets the returned resultset values into the child bean properties.  Each bean is added to an ArrayList which is a property of the parent.  There is a getter which returns this ArrayList. Each child also has appropriate getters for its properties.  I have tested that the parent correctly creates the ArrayList of children by using a separate Java class to instantiate the parent and access the properties of its children.  

I can successfully set and get the parent's String property from my jsp page, but cannot figure out how to iterate over the ArrayList of child beans using c:forEach.  My goal is to build a table to show the child bean property values.  It seems to me it all boils down to a simple collection of objects whose properties I want to access -- something that I thought forEach could do.  Is this possible?  If so, how?  An example would be really helpful.
Thanks in advance for any insight into this.

  • 3
  • 2
1 Solution
Surprised noone has answered you...

<c.forEach var="item" items="${parentBean.children}">
<c:out value="${item.property}"/>

That's assuming your parent bean is in the request/page/session/application scope under the name "parentBean" and that it has a method called getChildren() to return the ArrayList.

If you've already worked this out (hope so) in the 9 days since asking the question ask a moderator to close and refund the points.

twr999Author Commented:
Thanks, searlas.  Not sure if my lengthy narrative without code examples scared folks off, or if it just isn't an interesting enough topic.  Anyway, I did solve this several days ago.  I think you should still get the points, since you were kind enough to answer.  

In the process of working this out, I discovered that I can carry it farther than I had hoped.  It is possible to capture the row number in the table and use it to access a specific child element, and then children of the child.

I have a jsp page with a table that lists all the products in a catalog and captures the row number at the same time.  The catalog bean (session scope) returns an array of product beans.  I iterate over these to build my table as follows:  

<c:forEach var="cd" items="${CDs.listCDs}" varStatus="rowNum" >
     <td><form action="../controller.jsp" method="post">
          <input type="hidden" name="indx" value="${rowNum.count - 1}" />
          <input type="hidden" name="event" value="cdViewDetails" />
          <input type="submit" value="View Details"/>

When the user clicks the "View Details" button, the row number is passed to the detail page, making it possible to retrieve the product and display its details:

<c:set var="i" value="${param.indx}" />
<td><h3 class=thin>${fn:escapeXml(catalog.listCDs[i].productName)}</h3> </td>

listCDs is an array property of the catalog bean that holds the product beans.  Each product bean also holds a reference to an array of "track beans", which can be accessed using another forEach loop:

<td ><c:forEach items="${catalog.listCDs[i].allTracks}" var="track">
     <c:out value="${fn:escapeXml(track.trackName)}" /><br>

I'm not sure if Sun documents the above anywhere -- if so, I couldn't find it.  I found out about the count property of varStatus in code examples posted on other sites on the Web.

I'm sure all this is pretty basic stuff to you, but maybe it will help another jsp newbie like me.  

Thanks for the points and the extensive feedback.  

Sun do provide documentation of this kind of thing (the JSTL spec), but it only contains the most basic examples; nothing like the real world scenario you present above.  You're example brings together a lot that someone new to JSTL needs to learn - parameter passing, iteration, array indexing etc.  I've yet to find a site or document with such a good example.
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Just curious, I work with JSP at work and none of c: and stuff like ${catalog.listCDs[i].allTracks}"  is familar to me.  Is that for scripts?  Or Struts?  What is a Strut anyway?
From: http://struts.apache.org/faqs/kickstart.html#struts

Why is it called Struts?
It's a reference to struts in the architectural sense, a reminder of the nearly invisible pieces that hold up buildings, houses, and bridges.

The stuff like ${catalog.listCDs[i].allTracks} is EL syntax from the latest JSP specifications (EL - 'Expression Language')

See Sun's tutorial on the expression language
... and Struts documentation on their support

twr999Author Commented:
I would add that the "c:forEach" is part of JSTL, the JSP Standard Template Library, which is designed to make it a little easier to write JSP code.

I also found some good information here: http://java.sun.com/webservices/docs/ea2/tutorial/doc/JSTL.html.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now