Solved

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

Posted on 2004-04-12
6
8,857 Views
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.

 
0
Comment
Question by:twr999
[X]
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
6 Comments
 
LVL 7

Accepted Solution

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

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

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.

0
 

Author Comment

by:twr999
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"/>
     </td></form>
...
</c:forEach>

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>
</c:forEach></td>

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.  


0
 
LVL 7

Expert Comment

by:searlas
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.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:SweetChastity
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?
0
 
LVL 7

Expert Comment

by:searlas
ID: 11527233
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
http://java.sun.com/j2ee/1.4/docs/tutorial/doc/JSPIntro7.html
... and Struts documentation on their support
http://struts.apache.org/faqs/struts-el.html

0
 

Author Comment

by:twr999
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: http://java.sun.com/webservices/docs/ea2/tutorial/doc/JSTL.html.


 
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupSum6 challenge 6 119
allswap challenge 6 148
listing all functions in JavaScript 19 248
Eclipse for Java EE development 2 67
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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.

738 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