Multiple-level nesting with <logic:iterate> in Struts html

Multiple-level nesting with <logic:iterate> in Struts

I expected multiple-level nesting with the <logic:iterate> tag in Struts to be difficult, but on the contrary it turned out to be an almost trivial exercise.

In my case/this example, we get from the data tier a list of books in a form bean as a collection of BookDTOs (Data Transfer Objects). A BookDTO contains fields such as title, publisher, year and a collection of authors (second iterator!), which in turn contains fields such as first name, last name and initials.

The JSP fragment is as follows:

<%@ page language="java" %>
<!DOCTYPE HTML PUBLIC "-//w3c//dtd html 4.0 transitional//en">
<%@ taglib uri="/tags/struts-html" prefix="html"%>
<%@ taglib uri="/tags/struts-bean" prefix="bean"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<logic:greaterThan property="numResults"
                   name="publicationsFormBean"
                   value="0" >
  <h3>Books in database:</h3>
  <ol>
    <logic:iterate
     name="publicationsFormBean"
     property="publications"
     indexId="i"
     id="dummy">
      <li>
        <i>
          <bean:write
           name="publicationsFormBean"
           property='<%= "publications["+ i +"].title" %>'/>
        </i>,
        <logic:iterate name="publicationsFormBean"
         property='<%= "publications["+ i +"].authors" %>'
         indexId="j"
         id="dummy2">
          <bean:write
           name="publicationsFormBean"
           property='<%= "publications["+ i +"].authors["+ j +"].initials" %>'/>
          <bean:write
           name="publicationsFormBean"
           property='<%= "publications["+ i +"].authors["+ j +"].lastName" %>'/>
         (<bean:write
           name="publicationsFormBean"
           property='<%= "publications["+ i +"].authors["+ j +"].firstName" %>'/>
          <bean:write
           name="publicationsFormBean"
           property='<%= "publications["+ i +"].authors["+ j +"].middleNames" %>'/>),
        </logic:iterate>
        <bean:write
         name="publicationsFormBean"
         property='<%= "publications[" + i + "].publisher" %>'/>,
        <bean:write
         name="publicationsFormBean"
         property='<%= "publications[" + i + "].year" %>'/>
      </li>
    </logic:iterate>
  </ol>
</logic:greaterThan>

Note how in the second iterator (with indexId=j) the property is desginated a collection in a collection. From there the properties can be addressed straightforwardly by separating them with dots.

This results in an output as follows:

  1. Book title one, J.W. Klaassen (Jan Willem), My Publisher, 2004
  2. Book title two, J.W. Klaassen (Jan Willem), P. Puk (Pietje), My Publisher, 2004

19 Comments

  1. Pingback: Another Blog Title March 17, 2011
  2. Anoop August 16, 2007
  3. bharti May 3, 2007
  4. freekyfashions July 14, 2006
  5. ankur June 28, 2005
  6. Rajamani David March 8, 2005
  7. Jose Tharail Raphael January 9, 2005
  8. Jet December 20, 2004
  9. pramod December 13, 2004
  10. Perogi December 10, 2004
  11. Jasper December 8, 2004
  12. Perogi December 8, 2004
  13. Perogi November 25, 2004
  14. Perogi November 25, 2004
  15. leon October 25, 2004
  16. Rajamani David October 24, 2004
  17. Lucas August 30, 2004
  18. Zeger Hendrikse August 26, 2004
  19. Jasper August 25, 2004
  20. Leon van Tegelen August 25, 2004