Sandeep is a ITA at Tata Consultancy Services. He also is the author of java blog. Sandeep has posted 24 posts at DZone. You can read more from them at their website. View Full User Profile

How to Iterate ArrayList in Struts2

  • submit to reddit

We will discuss how to iterate over a collection of String objects in Struts2 tag libraries and then a List of custom class objects. It looks as if iterating a list of string objects is easier than iterating over a list of custom class objects in Struts 2. But the reality is that iterating a list of custom class objects is also equally easier. By custom class we mean the User, Employee, Department, Products, Vehicles classes that are created in any web application.

Download Working Sample Here
Usually it happens that one needs to fetch a list of records from database/files and then display it in the JSP. The module requiring this functionality could be Search, Listing users/departments/products etc.
The basic flow of struts2 web application goes like:The user initiates the request from one page. This request is received by the interceptor which further invokes the Struts2 action. The action class fetches the records and stores in a list. This list is available to the next JSP using the public getter method.Please note that the public getter method for the List is mandatory.
Once the List has been populated by Struts2 action class, the JSP then iterates over this List and displays the corresponding information. In the days gone by, one would store the List as a session attribute and then access the list in JSP using the scriptlets to display appropriate output to the users.
Here is a Struts2 sample application to iterate one String and one custom class objects List.Though we are using the Struts2 tag library to iterate the list but JSTL can also be used for iteration.
Also if you are going to use  the code examples given below, use the following URL's to access the application:

Iterate a Custom class ArrayList in Struts2
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" xmlns="" xmlns:web="" xsi:schemaLocation="" version="2.5">
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "">

<package name="default" extends="struts-default" >

<action name="index">
<result name="success">/home.jsp</result>
<action name="fetch" class="com.example.FetchAction">
<result name="success">/success.jsp</result>
<result name="failure">/failure.jsp</result>


<%@taglib uri="/struts-tags" prefix="s" %>

Enter a user name to get the documents uploaded by that user.
<s:form name="fetch" theme="simple" action="fetch">
Username <s:textfield name="username" />
<s:submit value="Fetch Records" />


<%@taglib uri="/struts-tags" prefix="s" %>


Documents uploaded by the user are:</br>
<s:iterator value="documents"><s:property value="name" /></br></s:iterator>
<%@taglib uri="/struts-tags" prefix="s" %>
<h3><s:property value="message" /></h3>
package com.example;

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

public class FetchAction {

private String username;
private String message;
private List documents = new ArrayList();

public List getDocuments() {
return documents;

public String getMessage() {
return message;
public void setMessage(String message) {
this.message = message;
public String getUsername() {
return username;
public void setUsername(String username) {
this.username = username;

public String execute() {

if( username != null) {
//logic to fetch the document list (say from database)
Document d1 = new Document();
Document d2 = new Document();
Document d3 = new Document();


return "success";
} else {
message="Unable to fetch";
return "failure";

package com.example;

public class Document {

private String name;

public String getName() {
return name;

public void setName(String name) { = name;


Iterate String List in Struts2

The way to iterate the a String list is similar with the only difference that the action class now populates the name of documents into an ArrayList of String objects.

The code zip file containing the iteration over an ArrayList of  custom class object or bean can be downloaded at:

The URL to access this application will be:

The code zip file containing the iteration over an ArrayList of  string class object or bean can be downloaded at:

The URL to access this application will be:



Published at DZone with permission of its author, Sandeep Bhandari.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)



Alessandro Santini replied on Tue, 2011/05/17 - 10:37am

The guy doesn't stop. Sandeep, what goal is this post intended to achieve? To show some basic skills with an obsolete framework? Is this what you mean with Extreme Java? Can't you take some inspiration from other (far more brilliant) posts here on DZone?

Avdhesh Yadav replied on Tue, 2011/05/17 - 12:18pm

Such posts are complete waste of time.It seems he just wants to get traffic to his blog. 

Hussachai Purip... replied on Tue, 2011/05/17 - 1:02pm

Totally waste! I don't know what is your purpose to post this article? In my opinion it's not even useful for beginner because there is no sign of interesting in it.

Wujek Srujek replied on Tue, 2011/05/17 - 1:35pm

Guys, I agree that this post is a total waste of time. However, it's not Sandeep who posted it here, is it? He has his own blog, and someone else (lobby leaders?) decided that it is worth being put here for all. I think you are directing your unfavorable comments to a wrong direction.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.