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"%>

<title>Students Details</title> 
<style type="text/css">
.odd
{
background-color: silver;
}
.even
{
background-color: white;
}
</style>
<html>
<body>
<h4>Simple Iterator</h4>
<table>
<tr>
    <td width="20%">RollNo</td>
    <td width="20%" >Student Name</td>
    <td width="20%">Department</td>
    <td width="20%">Percentage</td>
</tr>

<s:iterator value="students">
<tr>
    <td width="20%"><s:property value="rollNo" /></td>
    <td width="20%" ><s:property value="studentName"/></td>
    <td width="20%"><s:property value="department"/></td>
    <td width="20%"><s:property value="percentage"/></td>
</tr>
</s:iterator>
</table>
<br/>

<h4>Iterator with Iterator Status</h4>
<table>
<tr>
    <td width="10%">So No</td>
    <td width="20%">RollNo</td>
    <td width="20%" >Student Name</td>
    <td width="20%">Department</td>
    <td width="20%">Percentage</td>
</tr>

<s:iterator value="students" status="rowstatus">
<tr class="<s:if test="#rowstatus.odd == true ">odd</s:if><s:else>even</s:else>">
    <td><s:property value="#rowstatus.count"/></td>
    <td width="20%"><s:property value="rollNo" /></td>
    <td width="20%" ><s:property value="studentName"/></td>
    <td width="20%"><s:property value="department"/></td>
    <td width="20%"><s:property value="percentage"/></td>
</tr>
</s:iterator>
</table>
</body>
</html>

 
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<StudentBean> students = new ArrayList<StudentBean>();

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<StudentBean> getStudents() 
{
	return students;
}

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

}

 
Demo
 
Iterator Struts2
 
Also read :

 
download
 

About Mohaideen Jamil


Am currently working as a Struts 2 Developer in a reputed IT Organisations. I can help you with teaching Core java and Struts 2. Follow me on Facebook or Google Plus. If you like my tutorials, consider making a donation to this charity, thanks.