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.  
JSP

Avatar of undefined
Last Comment
summerian

8/22/2022 - Mon
suprapto45

You should have the property attributes in your <bean:write>

https://www.experts-exchange.com/Web/Web_Languages/JSP/Q_20899554.html
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>

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
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
summerian

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.
summerian

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>).

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.

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
summerian

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.

AKnibbs

ASKER
thanks for that info - any idea as to why it would continuously say that it can't find the bean oneResult then ?
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 ?
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
ASKER CERTIFIED SOLUTION
summerian

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
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.
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 ?
AKnibbs

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

thanks so much
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
summerian

it happens ;)