Link to home
Start Free TrialLog in
Avatar of AKnibbs
AKnibbs

asked on

Stuts +iterating over a String array

Hi all I am working with struts and need to be able to display the contents of a string array in the browser.  I am trying to use the following code :

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

SkuSearchForm is the name of the form where the array is and results is the name of the String array.  When I execute the page that has the above in it there is no error message.  The page comes back with the memory address where the string array is stored (or at least that is my suspicion).  Any thoughts or ideas on this would be much appreciated.  
Avatar of suprapto45
suprapto45
Flag of Singapore image

Avatar of AKnibbs
AKnibbs

ASKER

That changed the result but it is still not correct.  
I get the following output on the page:
[Ljava.lang.String;@c194ad  
When using the code below:
<logic:iterate id="oneResult" name="SkuSearchForm" property="results" >
<tr>
<td>
<bean:write name="SkuSearchForm" property="results" />
</td>
</tr>
</logic:iterate>

Avatar of AKnibbs

ASKER

After a little more poking aroud I realized a very stupid mistake - I forgot to include the tablib for logic.  I am still not getting the values out of the string, but at least now it is iterating over the size of it (unfortunately the output is the same everytime, so it's not just an issue of printing the string address
Your code should read:

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

That is, of course if 'results' is a property holding an array/list of strings. If not, specify property to point the getter of the object in <bean:write> tag, to retrieve the value for output.
To clear things out:
In your code, <bean:write> tag gets to output an array of strings, so its an equivalent of:

String[] array = new String[3];
System.out.println(array);

But you want to output each string in each iteration of the <logic:iterate> tag, which is saved in bean named 'oneResult' (the 'id' attribute in <logic:iterate>).

Avatar of AKnibbs

ASKER

I don't quite follow where oneResult comes in.  I don't have a bean named oneResult - should that be getter and setter methods in my SkuSearchForm ? Or is it supposed to be a new bean entirely on it's own. Any thoughts/suggestions/examples would be appreciated.

Bean named 'oneResult' is introduced by the <logic:iterate> tag. It doesn't have antyhing to do with getters and setters in SkuSearchForm. The form, however, must have getter for 'results' property, which must be an instance of an array or collection of objects.

Try to think about <logic:iterate> tag as follows:

(pseudo code)
for-each 'oneResult' in 'SkuSearchForm.results' do:
   write oneResult

Where <logic:iterate> is a for-each loop, 'oneResult' is a variable/bean, which gets an element from 'results' collection/array in each iteration.

<bean:write> just prints the bean/variable (which is in this case a String object, so no getter property is needed) to the page output stream.

Avatar of AKnibbs

ASKER

thanks for that info - any idea as to why it would continuously say that it can't find the bean oneResult then ?
Avatar of AKnibbs

ASKER

the exact error is:

Cannot find bean: "oneResult" in any scope
javax.servlet.ServletException: javax.servlet.jsp.JspException: ServletException in '/searchResults.jsp': Cannot find bean: "oneResult" in any scope

Is there something specific I should be setting with respect to the scope ?
ASKER CERTIFIED SOLUTION
Avatar of summerian
summerian
Flag of Poland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of AKnibbs

ASKER

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

that is the code used, and I have both the imports for the jstl as well as the logic taglibs at the top of the page.  Again i'm sure it's something extremely stupid that I am doing incorrectly.
Avatar of AKnibbs

ASKER

Here's a possibility that I have to consider is there a way to ensure that the entire array isn't null and that I am not placing values in it properly ?
Avatar of AKnibbs

ASKER

It was me being a complete moron and not placing the String array in the right variable.

thanks so much
it happens ;)