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

Posted on 2004-04-12
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.

Question by:twr999
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2

Accepted Solution

searlas earned 250 total points
ID: 10876398
Surprised noone has answered you...

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

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.


Author Comment

ID: 10880400
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.  


Expert Comment

ID: 10886616
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.
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.


Expert Comment

ID: 11523929
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?

Expert Comment

ID: 11527233

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


Author Comment

ID: 11530084
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:


Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question