Struts 2 Iterator Tag Example

Struts 2 iterator tag is useful for iterating a value which of type java.util.Collections or java.util.Iterator. This example demonstrates with a simple iteration block which is taking the users list and displaying the list of users in the list.

1. Create Data Bean

Create a UserDetails.java bean to hold the user details.

package javabeat.net.struts2;

public class UserDetails {
	private String name;
	private String city;
	public UserDetails(String... args){
		this.name = args[0];
		this.city = args[1];
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}

}

2. Create Struts 2 Action

Create a action class for storing the values and passing the UserDetails object to the screen.

package javabeat.net.struts2;

import java.util.ArrayList;

public class Struts2Iteration {
	private ArrayList<UserDetails> users;

	public ArrayList<UserDetails> getUsers() {
		return users;
	}

	public void setUsers(ArrayList<UserDetails> users) {
		this.users = users;
	}
	public String execute(){
		users = new ArrayList<UserDetails>();
		users.add(new UserDetails("Krishna","Bangalore"));
		users.add(new UserDetails("Rahul","Bangalore"));
		users.add(new UserDetails("Arjun","Bangalore"));
		return "success";
	}
}

3. Iterator Example

Here is the snippet for writing the iterator tag in your JSP. It is taking the list of objects and passing to the tag, property sub element under the iterator tag displays the value stored in the each field. It is very simple compare to the previous version of struts framework.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Struts 2 Iteration Example</title>
</head>
<body>
   <B>Struts 2 Iteration Example</B>
   <form action="iterationexample">
     <s:iterator value="users">
		<s:property value="name"/> ,
		<s:property value="city"/><br/>
	 </s:iterator>
   </form>
</body>
</html>

4. Struts.xml configurations

Create a simple struts.xml configuration file with action mappings.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
   "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
   "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
	<constant name="struts.devMode" value="true" />
	<package name="tags" extends="struts-default">
		<action name="iterationexample" class="javabeat.net.struts2.Struts2Iteration"
			method="execute">
			<result name="success">/index.jsp</result>
		</action>
	</package>
</struts>

5. Run the application

If you access the application http://localhost:8080/Struts2App/iterationexample.action. You would see the following output in your screen.

Struts 2 Iterator Tag Example Screen

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*

Close
Please support the site
By clicking any of these buttons you help our site to get better