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 struts2 iterator tag Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "struts2 iterator tag" Watch "struts2 iterator tag" New topic
Author

struts2 iterator tag

Rajani Panchumarthy
Greenhorn

Joined: Jan 27, 2009
Posts: 22
Hi
I am having an action class where I return an object list of users(like name,email,etc) fetched from DB.
using <s:iterator> tag in struts2, how can i display the table data in jsp.
Thanks in advance
Balagopal Kannampallil
Ranch Hand

Joined: Oct 18, 2004
Posts: 136

Hi Rajani,
I am going to explain it with an example. Hope this will clear your doubt.

First create an simple POJO, for example let us call it Employee. Let the Employee object hold the name, email, and age. You employee class will look like this


In the DAO implementation you can write a method like this (I am just writing an outline)

Now in the action class, make a form attribute of type List<Employee> (let us call it myEmpList) . Make its corresponding getter and setter method. After this you can call the getEmployeeList() method to get the list of employees and set it to myEmpList. So now myEmpList holds employee objects you populated from database.
Now in the jsp, we are going to make a table containing employee objects in different rows using s:iterator like this


SCJP 5.0 and now fighting with ExtJS
Rajani Panchumarthy
Greenhorn

Joined: Jan 27, 2009
Posts: 22
Hello,
Thank you for the explanation.
I followed the steps above but I am able to display only the last record from the myEmpList.
Why is this behaviour any ideas?
Balagopal Kannampallil
Ranch Hand

Joined: Oct 18, 2004
Posts: 136


* Is your DAO class fetching the values properly from the database?
* Iterate myEmpList in your action class and print the contents? Is it printing all the values as you expected?
Rajani Panchumarthy
Greenhorn

Joined: Jan 27, 2009
Posts: 22
When I iterate over the emplist, an object like Employee@1f30035 is printed.
In getEmployees() method of DAO class, the following is the way i add the employee objects
Employee e = new Employee();
while(rs.next()){
e.setEmployeeId(rs.getString(1));
e.setName(rs.getString(2));
e.setProject(rs.getString(3));
empList.add(e);
}
I have three rows in my table, and only the last record in the table is printed in jsp three times.
Do I have iterate over the list in iterator tag using soem index.
How do I have to write the OGNL expresion in iterator tag?
Rajani Panchumarthy
Greenhorn

Joined: Jan 27, 2009
Posts: 22
When I print the values in action class, only the last record is printed.
Only the last record in the list is being displayed in jsp
Rajani Panchumarthy
Greenhorn

Joined: Jan 27, 2009
Posts: 22
ok, i identified the problem.
I was trying to always add to the same employee object. that was the problem.

Balagopal Kannampallil
Ranch Hand

Joined: Oct 18, 2004
Posts: 136

Small mistake Rajani. Rewrite your code like this

In the code you have written. Only one employee object is created and inserted into the list.
Balagopal Kannampallil
Ranch Hand

Joined: Oct 18, 2004
Posts: 136

ok, i identified the problem.
I was trying to always add to the same employee object. that was the problem.

cool
Rajani Panchumarthy
Greenhorn

Joined: Jan 27, 2009
Posts: 22
Thank you for your prompt answers.
I would like to add pagination to my results that are being displayed in jsps.
in struts2, do I get any tag that serves this purpose.
Balagopal Kannampallil
Ranch Hand

Joined: Oct 18, 2004
Posts: 136

Sorry Rajani, I have not worked on pagination.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: struts2 iterator tag