File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes Struts iteration over String array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts iteration over String array" Watch "Struts iteration over String array" New topic
Author

Struts iteration over String array

A knibbs
Ranch Hand

Joined: Aug 23, 2006
Posts: 158
Hi all I am new to struts, currently working on getting a string array printed out on a JSP. As it stands right now the address of the array is being printed, not the individual elements in the array. Can someone give me an idea as to what I am doing wrong.

thanks in advance.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
There are two ways of doing this:

1- Use JSTL with combination of <c:forEach> and <cut> tags. Example:

<c:forEach var="element" items="${myStringArray}">
<cut value="${element}" />
</c:forEach>

2- Use Struts tags with a combination of <logic:iterate> and <bean:write> tags. Example:

<logic:iterate id="element" name="myStringArray" >
<bean:write name="element" />
</logic:iterate>
[ September 13, 2006: Message edited by: Merrill Higginson ]

Merrill
Consultant, Sima Solutions
A knibbs
Ranch Hand

Joined: Aug 23, 2006
Posts: 158
Originally posted by Merrill Higginson:
There are two ways of doing this:

1- Use JSTL with combination of <c:forEach> and <c ut> tags. Example:

<c:forEach var="element" items="${myStringArray}">
<c ut value="${element}" />
</c:forEach>

2- Use Struts tags with a combination of <logic:iterate> and <bean:write> tags. Example:

<logic:iterate id="element" name="myStringArray" >
<bean:write name="element" />
</logic:iterate>

[ September 13, 2006: Message edited by: Merrill Higginson ]


I am trying to use the second version, and I am putting

<logic:iterate id="SkuSearchForm" name="results">
<tr>
<td>
<bean:write name="SkuSearchForm" />
</td>
</tr>
</logic:iterate>

where SkuSearchForm is the bean where the data resides and results is the name of the string array where it is stored, yet I am still getting the address (of the form SkuSearchForm@fa97a7) when I try it - is there anything obviously wrong that I am doing here ?
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
The id attribute is for the individual object created with each iteration. So, obviously, the AcitonForm does not go there. If the collection or array you are iterating over is a property of your ActionForm bean, you must specify the ActionForm bean as the name and the property name as the property.

In your example, you would write:

<logic:iterate id="element" name="skuSearchForm" property="results">
<bean:write name="element" />
</logic:iterate>
[ September 13, 2006: Message edited by: Merrill Higginson ]
A knibbs
Ranch Hand

Joined: Aug 23, 2006
Posts: 158
Originally posted by Merrill Higginson:
The id attribute is for the individual object created with each iteration. So, obviously, the AcitonForm does not go there. If the collection or array you are iterating over is a property of your ActionForm bean, you must specify the ActionForm bean as the name and the property name as the property.

In your example, you would write:

<logic:iterate id="element" name="skuSearchForm" property="results">
<bean:write name="element" />
</logic:iterate>

[ September 13, 2006: Message edited by: Merrill Higginson ]


I have tried the above and it tells me that it cannot find bean "element" in any scope. I am sure it's something that I have done incorrectly. I'm just not sure what. Is there fuctions that I need other than supplying the getter and setter for the string array or anything particular to the page that is required ?
[ September 14, 2006: Message edited by: A knibbs ]
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
On thing that has gotten me a number of times is forgetting to add the taglib line to my jsp.

- Brent
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Good point, Brent. This error would occur if you included the definition of the bean tag library, but omitted the definition for the logic tag library.
A knibbs
Ranch Hand

Joined: Aug 23, 2006
Posts: 158
Originally posted by Merrill Higginson:
Good point, Brent. This error would occur if you included the definition of the bean tag library, but omitted the definition for the logic tag library.

I actually did that, and fixed that error, but it is still telling me that it can't find the bean element anywhere - am I forgetting something else ?
Do I need a seperate bean just to display the values or is it a getter/setter problem ? It's been frustrating me to no end as to why this isn't working.

thanks for all the ideas thus far.
A knibbs
Ranch Hand

Joined: Aug 23, 2006
Posts: 158
I knew it was me making a simple mistake. I wasn't placing the variable in the right location.

thanks for all the help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Struts iteration over String array