Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

struts2 iterator tag

 
Rajani Panchumarthy
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 136
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Rajani Panchumarthy
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 136
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

* 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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, i identified the problem.
I was trying to always add to the same employee object. that was the problem.

 
Balagopal Kannampallil
Ranch Hand
Posts: 136
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 136
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, i identified the problem.
I was trying to always add to the same employee object. that was the problem.

cool
 
Rajani Panchumarthy
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 136
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry Rajani, I have not worked on pagination.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic