DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • SmartXML: An Alternative to XPath for Complex XML Files
  • Java String: A Complete Guide With Examples
  • Generics in Java and Their Implementation
  • Ultimate Guide to FaceIO

Trending

  • Scaling DevOps With NGINX Caching: Reducing Latency and Backend Load
  • Beyond Simple Responses: Building Truly Conversational LLM Chatbots
  • A Deep Dive Into Firmware Over the Air for IoT Devices
  • Start Coding With Google Cloud Workstations
  1. DZone
  2. Data Engineering
  3. Data
  4. How to Iterate ArrayList in Struts2

How to Iterate ArrayList in Struts2

By 
Sandeep Bhandari user avatar
Sandeep Bhandari
·
May. 17, 11 · Interview
Likes (0)
Comment
Save
Tweet
Share
70.7K Views

Join the DZone community and get the full member experience.

Join For Free

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:
http://localhost:8080//index.action


Iterate a Custom class ArrayList in Struts2

web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name></display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
</web-app>
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>

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

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

</package>

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

<html>
<body>
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" />
</s:form>

</body>
</html>

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

<html>

<body>
Documents uploaded by the user are:</br>
<s:iterator value="documents"><s:property value="name" /></br></s:iterator>
</body>
</html>
 
failure.jsp
<%@taglib uri="/struts-tags" prefix="s" %>
<html>
<body>
<h3><s:property value="message" /></h3>
</body>
</html>
FileAction.java
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();
d1.setName("user.doc");
Document d2 = new Document();
d2.setName("office.doc");
Document d3 = new Document();
d3.setName("transactions.doc");

documents.add(d1);
documents.add(d2);
documents.add(d3);

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

Document.java

package com.example;

public class Document {

private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.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 FetchAction.java 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:
http://www.fileserve.com/file/QmrsJ7k

The URL to access this application will be:
http://localhost:8080/IteratorExample/index.action

The code zip file containing the iteration over an ArrayList of  string class object or bean can be downloaded at:
http://www.fileserve.com/file/V2kXkfx

The URL to access this application will be:
http://localhost:8080/StringIteratorExample/index.action

 

From http://extreme-java.blogspot.com/2011/05/how-to-iterate-arraylist-in-struts2.html

Data structure Listing (computer) Web application Object (computer science)

Opinions expressed by DZone contributors are their own.

Related

  • SmartXML: An Alternative to XPath for Complex XML Files
  • Java String: A Complete Guide With Examples
  • Generics in Java and Their Implementation
  • Ultimate Guide to FaceIO

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: