aspose file tools*
The moose likes Struts and the fly likes Iterator problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Iterator problem" Watch "Iterator problem" New topic
Author

Iterator problem

michell o'brien
Ranch Hand

Joined: Jun 09, 2009
Posts: 37
I am not sure where i am going wrong. But my code below is accessiong the "menArraylist" 2wice and every time only the last element is being diplayed. when I try to display the values of menArrayList in the Action class it is showing me the right values. Can anybody tell me where i am going wrong.



and my menform class has

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61653
    
  67

Please be sure to ask Struts questions in the Struts forum. I have moved this post there for you.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
michell o'brien
Ranch Hand

Joined: Jun 09, 2009
Posts: 37
Can I have the link for the Struts forum please
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Bear Bibeault wrote:I have moved this post there for you.

I don't see anything obvious; you should post the rest of the code otherwise figuring anything out is essentially impossible.

Wait, did you say menArrayList is in your form bean? Isn't it supposed to be in scope?

Btw, it's recommended to use JSTL tags where functionality overlaps w/ Struts 1 tags.
michell o'brien
Ranch Hand

Joined: Jun 09, 2009
Posts: 37
menArraylist is the collection from Menform class. And it holds String values.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

That didn't really answer any of my questions.
michell o'brien
Ranch Hand

Joined: Jun 09, 2009
Posts: 37
Guys can anybody please help me in solving this issue. I have already spent enough time in this.
michell o'brien
Ranch Hand

Joined: Jun 09, 2009
Posts: 37
I am sorry Mr.David Newton then i did not get you?
michell o'brien
Ranch Hand

Joined: Jun 09, 2009
Posts: 37
My action class. "Menaction.java"




Menform.java



menu.jsp



David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

You're always adding the same object to the list.
michell o'brien
Ranch Hand

Joined: Jun 09, 2009
Posts: 37
sorry i did not get you
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

There are at least a couple of issues with this code:

1) Line 4 adds the same object to the list, over and over. Line 2 changes a property of the object you're adding, so you end up seeing the same value multiple times. This is almost certainly not what you want, and explains why you're seeing the same values.

2) Line 5: There's no reason to set the same list into the session over and over. Just do it once, outside of the loop.

(See how helpful posting the actual code was?)
michell o'brien
Ranch Hand

Joined: Jun 09, 2009
Posts: 37
Sir i understood he 2nd point what you mentioned but regarding the first one menlist is


so everytime i add a new value to menlist.setCategory_name I guess it should appened the value. This is what i thought it does.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I don't see that anywhere in your code. If it's not in the loop, you're not making a new object for every item in the result set. If it is in the loop, then why didn't you post the actual code?
michell o'brien
Ranch Hand

Joined: Jun 09, 2009
Posts: 37
I am sorry sir I had written that on top of my rs.next{

}

That was the problem. It was not creating a new object everytime.

Thankyou for being so humble to me and explaining the code what I had written.
Once again Thank you very much :-)
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

You're welcome--glad I could help.
 
 
subject: Iterator problem