Struts 2 Iterator Example

 Download this Project

In this article, we will learn how to iterate a list of Strings or Objects using iterator tag in struts2 . Below is the syntax for the iterator tag.

<s:iterator value="someList">
      <s:property />

1. Development Environment 

  1. Eclipse
  2. JDK 1.8
  3. Tomcat 8.5
  4. Maven

2. Create a Maven Web Project

Below is my dynamic web project struts2-iterator structure in eclipse. In this example, we are using struts 2.1.8 version.

This project has the following actions,

  • iterator-example.action: In this action, we will iterate a list of strings and a list of objects.


package com.simba.demo;

import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;

 * @author Simba
public class IteratorExampleAction extends ActionSupport{

  private List<String> listNames=new ArrayList<String>();
  private List<User> listUsers=new ArrayList<User>();;
  public List<String> getListNames() {
    return listNames;

  public void setListNames(List<String> listNames) {
    this.listNames = listNames;

  public List<User> getListUsers() {
    return listUsers;

  public void setListUsers(List<User> listUsers) {
    this.listUsers = listUsers;

  public String iteratorExample() {
    listUsers.add(new User("Arya", "Female", 16));
    listUsers.add(new User("Sansa", "Female", 19));
    listUsers.add(new User("Jon", "Male", 22));
    listUsers.add(new User("Daenerys", "Female", 25));
    listUsers.add(new User("Tyrion", "Male", 35));
    return SUCCESS;
package com.simba.demo;

 * @author Simba
public class User {

  private String name;
  private String gender;
  private Integer age;

  public User(String name, String gender, Integer age) {
    super(); = name;
    this.gender = gender;
    this.age = age;
  public String getName() {
    return name;
  public void setName(String name) { = name;
  public String getGender() {
    return gender;
  public void setGender(String gender) {
    this.gender = gender;
  public Integer getAge() {
    return age;
  public void setAge(Integer age) {
    this.age = age;

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<%@taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Iterator Example</title>
  <h1>Iterator Example- List of String values</h1>

    <s:iterator value="listNames">
        <td><s:property /></td>

  <h1>Iterator Example- List of Objects</h1>
    <s:iterator value="listUsers">
        <td><s:property value="name" /></td>
        <td><s:property value="gender" /></td>
        <td><s:property value="age" /></td>

"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"


  <package name="iteratorExample" namespace="/" extends="struts-default">
    <action name="iterator-example" class="com.simba.demo.IteratorExampleAction" method="iteratorExample">

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi=""
  id="WebApp_ID" version="3.1">
<project xmlns="" xmlns:xsi=""

3. Testing

In your eclipse configure tomcat and run your application by selecting Run on server.