Struts 2 Iterator tag example

Struts 2 Iterator tag is used to iterate over a value, which can be any of java.util.Collection or java.util.Iterator. In this tutorials, you will create a list variable, use Iterator tag to loop over it and get the iterator status with IteratorStatus.

1. Action

An Action class with a List property , which contains variety of delicious “KFC combo meals”.


package com.mkyong.common.action;
import java.util.ArrayList;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
public class IteratorKFCAction extends ActionSupport{
	private List<String> comboMeals;
	public List<String> getComboMeals() {
		return comboMeals;
	public void setComboMeals(List<String> comboMeals) {
		this.comboMeals = comboMeals;
	public String execute() {
		comboMeals = new ArrayList<String>();
		comboMeals.add("Snack Plate");
		comboMeals.add("Dinner Plate");
		comboMeals.add("Colonel Chicken Rice Combo");
		comboMeals.add("Colonel Burger");
		comboMeals.add("O.R. Fillet Burger");
		comboMeals.add("Zinger Burger");
		return SUCCESS;

2. Iterator example

A JSP page to show the use of Iterator tag to loop over the “KFC comboMeals” List. In Iterator tag, it contains a “status” attribute, which is used to declared a name for IteratorStatus class.

The IteratorStatus class is used to get information about the status of the iteration. Supported properties are index, count, first, last, odd, even and etc..Make sure you visit this IteratorStatus documentation to know more details of it.
<%@ taglib prefix="s" uri="/struts-tags" %>
<h1>Struts 2 Iterator tag example</h1>
<h2>Simple Iterator</h2>
<s:iterator value="comboMeals">
  <li><s:property /></li>
<h2>Iterator with IteratorStatus</h2>
<s:iterator value="comboMeals" status="comboMealsStatus">
  	<s:if test="#comboMealsStatus.even == true">
      <td style="background: #CCCCCC"><s:property/></td>
    <s:elseif test="#comboMealsStatus.first == true">
      <td><s:property/> (This is first value) </td>

3. struts.xml

Link it ~

<?xml version="1.0" encoding="UTF-8" ?>
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
 	<constant name="struts.devMode" value="true" />
	<package name="default" namespace="/" extends="struts-default">
		<action name="iteratorKFCAction" 
			class="com.mkyong.common.action.IteratorKFCAction" >
			<result name="success">pages/iterator.jsp</result>

4. Demo


Struts 2 Iterator tag


  1. Struts 2 Iterator tag example
  2. IteratorStatus documentation
Tags :

About the Author

Founder of and, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.


  • Pingback: Blue Coaster33()

  • ahmed

    Hi I have some issue working with nested struts iterator i m put my issue in this link

  • Ayia Napa_s 5 Best Hotels

    Hi there friends, how is all, and what you want to
    say about this piece of writing, in my view its genuinely awesome designed for me.

  • Gaurav

    Here is an extension to the above tutorial.
    How to iterate over a List, Map and List using struts 2.

    • Gaurav

      It is actually List(of String), Map and List(of Objects)

  • nathan

    Hello Mkyong,

    In the iterator.jsp page you did not add the struts form tag. that’s why the output not coming. please add this tag to the iterator.jsp page

    the below is the correct version

    &lt;s:form action=&quot;iteratorKFCAction&quot; &gt; 
    &lt;h1&gt;Struts 2 Iterator tag example&lt;/h1&gt;
    &lt;h3&gt;Simple Iterator&lt;/h3&gt;
    &lt;s:iterator value=&quot;comboMeals&quot;&gt;
      &lt;li&gt;&lt;s:property /&gt;&lt;/li&gt;
    &lt;h3&gt;Iterator with IteratorStatus&lt;/h3&gt;
    &lt;s:iterator value=&quot;comboMeals&quot; status=&quot;comboMealsStatus&quot;&gt;
      	&lt;s:if test=&quot;#comboMealsStatus.even == true&quot;&gt;
          &lt;td style=&quot;background: #CCCCCC&quot;&gt;&lt;s:property/&gt;&lt;/td&gt;
        &lt;s:elseif test=&quot;#comboMealsStatus.first == true&quot;&gt;
          &lt;td&gt;&lt;s:property/&gt; (This is first value) &lt;/td&gt;

    now anyone can get the output, its working fine

  • nathan

    Struts 2 Iterator tag example
    Simple Iterator
    Iterator with IteratorStatus

    Hello sir, I could not get the output, what is the problem

  • nathan

    hello MkYong

    I go the below output only, What is the problem, why data not coming?

    Struts 2 Iterator tag example
    Simple Iterator
    Iterator with IteratorStatus

  • mahmoud

    hi, i m traying to iterat a lsit of object in my jsp i use this

    &lt;s:iterator value=&quot;hotelfound&quot; var=&quot;hot&quot;&gt;
                                    &lt;td&gt; &lt;s:property value=&quot;%{hot.hoName}&quot;/&gt;&lt;/td&gt;

    my model object contain other object

      public class Hotels {
    	private String hotel_id;
    	private String hoName;
    	private String hoDEscrip;
    	private HotelAdress ho_ddress;
    	private Integer tel1;
    	private Integer tel2;
    	private String imgs;
    	private String login;
    	private String password;
    	public String getLogin() {
    		return login;
    	public void setLogin(String login) {
    		this.login = login;
    	public String getPassword() {
    		return password;
    	public void setPassword(String password) {
    		this.password = password;
    	public String getHoName() {
    		return hoName;
    	public void setHoName(String hoName) {
    		this.hoName = hoName;
    	public String getHoDEscrip() {
    		return hoDEscrip;
    	public void setHoDEscrip(String hoDEscrip) {
    		this.hoDEscrip = hoDEscrip;
    	public void setHo_ddress(HotelAdress ho_ddress) {
    		this.ho_ddress = ho_ddress;
    	public HotelAdress getHo_ddress() {
    		return ho_ddress;
    	public void setTel1(Integer tel1) {
    		this.tel1 = tel1;
    	public Integer getTel1() {
    		return tel1;
    	public void setTel2(Integer tel2) {
    		this.tel2 = tel2;
    	public Integer getTel2() {
    		return tel2;
    	public void setImgs(String imgs) {
    		this.imgs = imgs;
    	public String getImgs() {
    		return imgs;
    	public void setHotel_id(String hotel_id) {
    		this.hotel_id = hotel_id;
    	public String getHotel_id() {
    		return hotel_id;

    and i get the list from MYSQL databases via an action strust2 and hibernate , spring
    but it doesn’t work any help

  • Joseph Kingston Leo

    Thank you it was very useful…………… Joseph (SCJP6)

  • Ravi Saini

    Dear sir
    i am using struts2 and i have a problem regarding to struts2

    problem:-I got data from mysql database to html table using iterator tag
    its working good for once.if i execute same action class again then repeated
    data add in same table.plz give me solution

  • Jeetendra biswas


  • Jeetendra biswas

    this is not working. what is this non sense thing is that those are not working….. this is bullshit!!

    • http://sdsad gd

      Nonsense this is Working!..

  • http://na skl

    I just get the below output..

    Struts 2 Iterator tag example
    Simple Iterator
    Iterator with IteratorStatus

    I am not getting any data ?? do you what the issue…
    I just copy pasted the above example

  • Arun

    hi mkyong !!

    useful u have any struts 1.2 jsp iterator tag i m a beginner,i m little bit struggling in this combination ..thanks


    • http://sdsad gd

      shutup !..

  • Oscar

    Very useful tuts!

    But i would need the same from an javabean’s attribute (as list of course) implemeting the modeldriven?


  • Pingback: Struts 2 Tutorials()