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 How to iterate through List of objects using OGNL <s:iterator> tag in struts2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "How to iterate through List of objects using OGNL <s:iterator> tag in struts2" Watch "How to iterate through List of objects using OGNL <s:iterator> tag in struts2" New topic
Author

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

varaprasad mutyala
Greenhorn

Joined: Jun 11, 2009
Posts: 17
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

Joined: Aug 03, 2008
Posts: 9313
    
  17

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


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
varaprasad mutyala
Greenhorn

Joined: Jun 11, 2009
Posts: 17
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

Joined: Aug 03, 2008
Posts: 9313
    
  17

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

Joined: Apr 01, 2009
Posts: 650
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

Joined: Apr 01, 2009
Posts: 650
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

Joined: Jun 11, 2009
Posts: 17

Thank You Ankit and Mark,

its working for me.
Nitin Surana
Ranch Hand

Joined: Jan 21, 2011
Posts: 129

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

Joined: Dec 31, 2013
Posts: 1
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................
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to iterate through List of objects using OGNL <s:iterator> tag in struts2