Struts 2 Iterator tag sample code

Below sample example you will see how to use Struts 2 iterator tag where we will print value from the list using property tag.

  • Create maven project name: Struts2Iterator
  • Project structure:

Struts 2 Iterator tag sample code

  • pom.xml dependency:
<dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>2.3.16.3</version>
        </dependency>

        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-convention-plugin</artifactId>
            <version>2.3.16.3</version>
</dependency>

 

  • IteratorTagExampleAction.java
package com.javahonk.action;

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

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.opensymphony.xwork2.ActionSupport;

@Results({
        @Result(name = "error", location = "/iterator.jsp"),
        @Result(name = "input", location = "/iterator.jsp")})
public class IteratorTagExampleAction extends ActionSupport {

    private static final long serialVersionUID = 1L;
    private List<Person> personList = new ArrayList<Person>();
    
    @Override
    @Action(value = "/iteratorTagExampleAction")    
    public String execute() throws Exception {
        
        personList.add(new Person("Java", "Honk", "NY"));
        personList.add(new Person("Ramesh", "Arrepu", "NY"));
        personList.add(new Person("Igor", "Vornovitsky", "NY"));
        personList.add(new Person("Bob", "Sidebottom", "NY"));
        personList.add(new Person("Sunil", "Garg", "NY"));
        personList.add(new Person("Mike", "Lee", "NY"));
        
        return ActionSupport.INPUT;
    }

    public List<Person> getPersonList() {
        return personList;
    }

    public void setPersonList(List<Person> personList) {
        this.personList = personList;
    }   
    
}

 

  • Person.java
package com.javahonk.action;

import java.io.Serializable;

public class Person implements Serializable{
    
    private static final long serialVersionUID = 1L;
    private String firstName;
    private String lastName;
    private String location;    
    
    public Person(String firstName, String lastName, String location) {
        super();
        this.firstName = firstName;
        this.lastName = lastName;
        this.location = location;
    }
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public String getLocation() {
        return location;
    }
    public void setLocation(String location) {
        this.location = location;
    }

}

 

  • iterator.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Struts 2 s:iterator tag sample code</title>
<s:head />
</head>

<body>
    <h2 style="color: green">
        <s:text name="label.welcome" />
    </h2>

    <h3 style="color: green">
        <s:text name="label.iterator" />
    </h3>
    <s:form action="iteratorTagExampleAction" namespace="/" method="post"
        name="strutsForm">
        <table cellspacing="0" cellpadding="0" border="1" width="500">
            <tr style="font-weight: bold">
                <td><s:text name="label.firstName" /></td>
                <td><s:text name="label.lastName" /></td>
                <td><s:text name="label.location" /></td>
            </tr>
            <s:iterator value="personList">
                <tr>
                    <td><s:property value="firstName" /></td>
                    <td><s:property value="lastName" /></td>
                    <td><s:property value="location" /></td>
                </tr>
            </s:iterator>
        </table>
    </s:form>
</body>
</html>

 

  • Output:

Struts 2 Iterator tag sample code

For more information please read this tutorial

download Download Project:  Struts2Iterator

Leave a Reply

Your email address will not be published. Required fields are marked *