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:
- Book title one, J.W. Klaassen (Jan Willem), My Publisher, 2004
- Book title two, J.W. Klaassen (Jan Willem), P. Puk (Pietje), My Publisher, 2004
Hi, Could anybody been able to retrieve values back from inner loop.
I tried hard but couldn’t got the solution, though i’m able to retrieve values
back from outer loop. What about the tag , could it be succesfully
developed and impleneted?