Solved

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

Posted on 2004-04-12
6
8,795 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
  • 3
  • 2
6 Comments
 
LVL 7

Accepted Solution

by:
searlas earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Expert Comment

by:SweetChastity
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Java Script nested call 3 53
java constructor error 8 86
scores100 challenge 3 82
wordsFront challenge 8 65
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 …
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.

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now