Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Iterate a String array in Struts

 
Kavita Chhabria
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All:

I have the following requirement:

I have a String array in java called String[] ListOfNames.

Each element in ListOfNames is again an array containing First Name, Middle Name and Last Name of individuals.

So, in other words, it is an array of arrays.

How do I iterate over this ListOfNames in struts such that, each internally contained string array is displayed as a single row in a table and as we iterate over the String array, for every new array contained within it, I am showing it as a new table row on the jsp page.

I hope I have my requirements clear.

Please ask for more explanation if needed.

Also, pls send me a soln to the above problem as soon as possible, I need this help urgently on my project.

Thanks a lot,

Kavita Chhabria
 
David Ulicny
Ranch Hand
Posts: 724
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will just two <logic:iterate> tags nested one in another.
Pseudo code:
<logic:iterate> <%-- Iterate over ListOfNames --%>
<logic:iterate>
Print inner array
</logic:iterate>
</logic:iterate>
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a String array in java called String[] ListOfNames.

Each element in ListOfNames is again an array containing First Name, Middle Name and Last Name of individuals.

Wouldn't that make listOfNames (which should be lower-case 'l', not 'L') a String[][]?
 
Robin Sharma
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kavita!

As Marc pointed out, you need to follow the bean law while naming variables in your ActionForm. So, assuming that the variable is called listOfNames, the following code should work fine:

<table border="1">
<logic:iterate id="outer" name="<Form key name>"
property="listOfNames">
<tr>
<logic:iterate id="inner" name="outer">
<td>
<bean:write name="inner"/>
</td>
</logic:iterate>
</tr>
</logic:iterate>
</table>
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic