We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now


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

twr999 asked
Medium Priority
Last Modified: 2013-11-24
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.

Watch Question

Unlock this solution and get a sample of our free trial.
(No credit card required)


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


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.

Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.