Struts2 framework provide iterator component to iterate all element which are add in the list.
<s:iterator></s:iterator>
The struts2 framework provide following tag:
<s:iterator value="studentlist" var="student">
<li><s:property /></li>
</s:iterator>
Directory Structure of <s:iterator> tag Example in Struts 2.0 Using MyEclipse IDE
index.jsp
<%@taglib uri="/struts-tags" prefix="s"%>
<h1>Iterator tag example in struts2 framework</h1>
<h3>Simple Iterator</h3>
<ol>
<s:iterator value="studentlist">
<li><s:property /></li>
</s:iterator>
</ol>
<h3>Iterator with IteratorStatus</h3>
<table>
<s:iterator value="studentlist" status="studentlistStatus">
<tr>
<s:if test="#studentlistStatus.even == true">
<td style="background: #CCCCCC"><s:property/></td>
</s:if>
<s:elseif test="#studentlistStatus.first == true">
<td><s:property/> (This is first value) </td>
</s:elseif>
<s:else>
<td><s:property/></td>
</s:else>
</tr>
</s:iterator></table>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<filter>
<filter-name>f1</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>f1</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration
2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="demo" extends="struts-default">
<action name="" class="org.r4r.IteratorAction">
<result name="success" type="dispatcher">/index.jsp</result>
</action>
</package>
</struts>
IteratorAction.java
package org.r4r;
import java.util.ArrayList;
import java.util.List;
public class IteratorAction {
List<String> studentlist;
public String execute(){
studentlist=new ArrayList<String>();
studentlist.add("Mukund Singh");
studentlist.add("Harsh Vardhan");
studentlist.add("Somaru Ram");
studentlist.add("Pramod Shakya");
studentlist.add("Sushant Tiwari");
studentlist.add("Gaurav Seth");
return "success";
}
public List<String> getStudentlist() {
return studentlist;
}
public void setStudentlist(List<String> studentlist) {
this.studentlist = studentlist;
}
}
Output