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 BookDTO
s (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?
I used the tag in my code, but getting exception as:
javax.servlet.ServletException: Cannot create iterator for this collection
Can you please suggest the possible causes for this exception???
Nice tip to learn abt the iteration.
I would like to add few more points which will give a very clear picture to others.
Here it is:
————
Create an arraylistContainer.
Create a bean book – [‘data1′,’data2′,’arrayList’]
‘arrayList’ – has another bean details – [‘bean1’]
‘bean1’ – has some attributes [‘data3′,’data4’]
Add the bean to ArrayListContainer.
Now start your iteration as followed:
—————————————
Hope this helps you all still better.
hi….
i am facing a problem regarding logic:itearate. i am getting the error
“cannot create iterator for the collection. i have made a multiple Select box as
my collection by declaring it as object of type java.util.string[] in my struts config
i am badly struck.
any help…..
ankur
Hi Perogi,
Good to see the solution for IndexedTextTag. I downloaded it from your website.
Do you have sample jsp to test IndexedTextTag? I could not find anything inside the zip file.
Thanks
Rajamani David
Hi,
I am having a problem with the logic:iterate tags.Below is the jsp code
When the page is loaded i get a compile error
[ServletException in:/repository/aim/view/addFunding.jsp] Unable to compile class for JSP An error occurred at line: 112 in the jsp file: /repository/aim/view/addFunding.jsp Generated servlet error: G:\jboss\server\default\work\jboss.web\localhost\digijava\org\apache\jsp\repository\aim\view\addFunding_jsp.java:343: incompatible types found : java.lang.String required: org.digijava.module.aim.helper.FundingDetail _jspx_fundingDetail_2 = fundingDetail; ^ An error occurred at line: 112 in the jsp file: /repository/aim/view/addFunding.jsp Generated servlet error: G:\jboss\server\default\work\jboss.web\localhost\digijava\org\apache\jsp\repository\aim\view\addFunding_jsp.java:349: incompatible types found : org.digijava.module.aim.helper.FundingDetail required: java.lang.String fundingDetail = (org.digijava.module.aim.helper.FundingDetail) _jspx_page_context.findAttribute(“fundingDetail”); ^ An error occurred at line: 112 in the jsp file: /repository/aim/view/addFunding.jsp Generated servlet error: G:\jboss\server\default\work\jboss.web\localhost\digijava\org\apache\jsp\repository\aim\view\addFunding_jsp.java:358: incompatible types found : org.digijava.module.aim.helper.FundingDetail required: java.lang.String fundingDetail = (org.digijava.module.aim.helper.FundingDetail) _jspx_page_context.findAttribute(“fundingDetail”); ^ An error occurred at line: 112 in the jsp file: /repository/aim/view/addFunding.jsp Generated servlet error: G:\jboss\server\default\work\jboss.web\localhost\digijava\org\apache\jsp\repository\aim\view\addFunding_jsp.java:362: incompatible types found : org.digijava.module.aim.helper.FundingDetail required: java.lang.String fundingDetail = _jspx_fundingDetail_2; ^ 4 errors ‘
When the page is loaded fundings and fundingDetails Collection is null.
I am stumped for now.
Any ideas.
Thanks in advance.
Hi all, this is an emergency, HELP PLEASE !
I have the following issue with loginc:iterate, does’n wokr properly, I mean doesn’t show anything in my browser..
mi logs doesn’t have any error message and everything seems to be fine…
Here the code:
/*******
< % views= (request.getAttribute("getViewRecordSet") != null ? (Vector) request.getAttribute("getViewRecordSet") : null); pageContext.setAttribute("views", views); %>
********/
erate>
I guess the property value are the gettes methods fom my action class…
So somebody can help ?
Thanks advanced !
nice
I am planning on making a indexedTextTag.war file to display its usage but for now, you can download the file at
IndexedTextTag. The link is on the left of the page under downloads, I hesitate to
include a direct link to the file in case I decide to add different downloads.
Hope this helps those, I know it helped me!
perogi.
Great, keep us posted!
I created a strutsish tag to enable this system. I am testing it atm and I will post a link to download it asap.
Hope this helps everyone!
perogi.
Doh! I just read Rajamani’s post. It appears that we have the same problem. If I locate a solution, I will post it.
Thanks again.
Perogi.
Hello! I currently am using ‘Leon’s’ technique. It displays perfectly but I have one major problem; the values for
the second iterated List are not being propagated back into the Object when ‘submit’ is clicked. Anyone else have this problem
and fixed it or have ideas on how to fix it?
Thanks in advance.
Perogi.
See the post by Jasper click here
Its great! Am using html:text in the second iterate. How do I capture the values after the submit. I tired all the different ways but its not working. Please help me to fix it.
However, should we not move to the JSTL c:foreach and c:out tags soon as possible?
I reworked my JSP according to Leon’s suggestions, and it worked flawlessly the first time! I’m impressed 🙂
Oops, i think this is a classic case of spreading bad code by copy-paste: in the BR-App the “[+i+]”-style indexing was neccesary while testing the struts indexed-properties, but it isn’t anymore. I forgot to remove it, sorry bout that..
By replacing the id attribute in the iterate tag by a meaningfull name, you can simply access the fields if you use that id as the name-attribute in the nested bean:write..
Will change the code in CVS right now
An alternative which in my opinion is a bit more readable is something like this:
this uses the id variables much more effective