• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to iterate through List of objects using OGNL <s:iterator> tag in struts2

 
varaprasad mutyala
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends,

I am using Struts 2 as part of My new Application.

I am using a List(java.util.List) of objects that will sent to view from Action class. These objects contains the data i have to display on View (jsp).

If it is a List of strings then i am using the following code its working fine for me.
<s:iterator value="myList">
<s:property/></p>
</s:iterator>

but my List contains Objects. I have to display data available in that Objects one by one by iterating through that List.

Can any body please help me out.

Thank you in Advance.


 
Ankit Garg
Sheriff
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but my List contains Objects. I have to display data available in that Objects one by one by iterating through that List.

What properties are there in these objects?? You can use the value attribute of the s:property tag to display a specific property of the object...
 
varaprasad mutyala
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ankit Garg thank you for your response..

My list contains user objects. each user has following 3 properties 1.id 2.name 3.role.

My List may Contains n number of user objects. I have to show all above details of users on by one in a jsp using <s:iterator> tag.
 
Ankit Garg
Sheriff
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That shouldn't be too hard. You just have to iterate over the list of objects and display the properties. It should work with a code similar to this (I hope I've not forgotten the syntax of struts tags )
 
Mark E Hansen
Ranch Hand
Posts: 650
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The value is interpreted as an OGNL expression, so you can just pass in the item member name, as in:
 
Mark E Hansen
Ranch Hand
Posts: 650
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I should point out that this works because in Struts 2, the element that is the target of the iteration (the item on the list) is pushed on to the Value Stack at the start of the loop (and popped-off at the end). This means that references to variable names that match those on the object will evaluate to the object on the Value Stack. It's like magic!

 
varaprasad mutyala
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thank You Ankit and Mark,

its working for me.
 
Nitin Surana
Ranch Hand
Posts: 129
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a working example(Netbeans 6.9 project) illustrating how to iterate over an array or list of objects.

Also, how to submit the form such that the list of objects gets re-created on submission.

Simply resolve the references and get going.

 
unia kasolia
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello guys

here is also a link that gives very good example to work with struts2 iterator tag
and i tried it it works very well

they also giving link to download code which is using eclipse IDE, Apache tomcat sever 7.0

go for it to take example, very nice one................
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic