Pages Navigation Menu

Coding is much easier than you think

iterator tag example in Struts 2

iterator tag example in Struts 2

 
Struts 2 Iterator will iterate over a value. An iterable value can be any of: java.util.Iterator,java.util.Collection.
In this tutorials, we will create a list and use Iterator tag to loop over it and get the iterator status with IteratorStatus.
 
Using IteratorStatus object one can get information about the status of the iteration, such as:

index: current iteration index, starts on 0 and increments in one on every iteration
count: iterations so far, starts on 1. count is always index + 1
first: true if index == 0
last: true if current iteration is the last iteration
even: true if (index + 1) % 2 == 0
odd: true if (index + 1) % 2 == 1
 
** UPDATE: Struts 2 Complete tutorial now available here.
 

Jsp -Iterator example
 

<%@taglib prefix="s" uri="/struts-tags"%>

Students Details



Simple Iterator

RollNoStudent NameDepartmentPercentage

Iterator with Iterator Status

oddeven">
So NoRollNoStudent NameDepartmentPercentage

 
Recommended reading :

 
Action Class
 

package com.simplecode.action;

import java.util.ArrayList;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
import com.simplecode.form.StudentBean;

public class StudentAction extends ActionSupport
{
private static final long serialVersionUID = 1L;
private List students = new ArrayList();

public String fetchStudentList()
{
students.add(new StudentBean("o7ba062", "Lavanya", "IT", 70));
students.add(new StudentBean("o7bb040", "Jaya Prakash", "ECE", 86));
students.add(new StudentBean("o7bd047", "Sundar", "MECH", 85));
students.add(new StudentBean("o7bb039", "Ibrahim Sha", "ECE", 85));
students.add(new StudentBean("o7bb009", "Aswin", "ECE", 84));
students.add(new StudentBean("o7ba027", "Nilafar", "IT", 82));
students.add(new StudentBean("o7bc079", "Nisha", "CSC", 81));
	
return SUCCESS;
}

public List getStudents()
{
	return students;
}

public void setStudents(List students)
{
	this.students = students;
}

}

 
Demo
 
Iterator Struts2
 
Also read :

 
download
 

10 Comments

  1. Nice and easy article however I am unable to see the values displayed in the table. I see it’s iterating through all records but not updating, any clues?

  2. Hi,

    1)I am using …. I need the values of the selected checkbox in my action class. I used an ArrayList but I am not able to get that. I am new to Struts2. Please help me

    2) For the above I used your code to implement a simple iterator . But I am not getting the values displayed. What could I be missing ?

  3. How can i update this table and save it in the database?

  4. Thanks brother..
    Nice Example

  5. Dear Jamil, This is a nice tutorial.Kindly upload a total sum example using iterator and nested iterator example.its urgent.

  6. Nice tutorial

  7. Nice tutorial

  8. Good example thanks