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 /> </s:iterator>
1. Development Environment
- Eclipse
- JDK 1.8
- Tomcat 8.5
- 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.
Code:
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() { listNames.add("Arya"); listNames.add("Sansa"); listNames.add("Jon"); listNames.add("Daenerys"); listNames.add("Tyrion"); 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(); this.name = name; this.gender = gender; this.age = age; } public String getName() { return name; } public void setName(String name) { this.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" pageEncoding="ISO-8859-1"%> <%@taglib uri="/struts-tags" prefix="s"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Iterator Example</title> </head> <body> <h1>Iterator Example- List of String values</h1> <table> <tr> <th>Name</th> </tr> <s:iterator value="listNames"> <tr> <td><s:property /></td> </tr> </s:iterator> </table> <h1>Iterator Example- List of Objects</h1> <table> <tr> <th>Name</th> <th>Gender</th> <th>Age</th> </tr> <s:iterator value="listUsers"> <tr> <td><s:property value="name" /></td> <td><s:property value="gender" /></td> <td><s:property value="age" /></td> </tr> </s:iterator> </table> </body> </html>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "classpath://struts-2.0.dtd"> <struts> <package name="iteratorExample" namespace="/" extends="struts-default"> <action name="iterator-example" class="com.simba.demo.IteratorExampleAction" method="iteratorExample"> <result>/WEB-INF/jsp/iterator-example.jsp</result> </action> </package> </struts>
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>struts2-iterator</display-name> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>struts2-iterator</groupId> <artifactId>struts2-iterator</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.7.0</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.0.0</version> <configuration> <warSourceDirectory>WebContent</warSourceDirectory> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>2.1.8</version> </dependency> </dependencies> </project>
3. Testing
In your eclipse configure tomcat and run your application by selecting Run on server.