Struts 2 Iterator tag example

Struts 2 Iterator tag is used to iterate over a value, which can be any of java.util.Collection or java.util.Iterator. In this tutorials, you will create a list variable, use Iterator tag to loop over it and get the iterator status with IteratorStatus.

1. Action

An Action class with a List property , which contains variety of delicious “KFC combo meals”.

IteratorKFCAction


package com.mkyong.common.action;
 
import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
 
public class IteratorKFCAction extends ActionSupport{
 
	private List<String> comboMeals;
	
	public List<String> getComboMeals() {
		return comboMeals;
	}

	public void setComboMeals(List<String> comboMeals) {
		this.comboMeals = comboMeals;
	}

	public String execute() {
		
		comboMeals = new ArrayList<String>();
		comboMeals.add("Snack Plate");
		comboMeals.add("Dinner Plate");
		comboMeals.add("Colonel Chicken Rice Combo");
		comboMeals.add("Colonel Burger");
		comboMeals.add("O.R. Fillet Burger");
		comboMeals.add("Zinger Burger");
		
		return SUCCESS;
	}
}

2. Iterator example

A JSP page to show the use of Iterator tag to loop over the “KFC comboMeals” List. In Iterator tag, it contains a “status” attribute, which is used to declared a name for IteratorStatus class.

The IteratorStatus class is used to get information about the status of the iteration. Supported properties are index, count, first, last, odd, even and etc..Make sure you visit this IteratorStatus documentation to know more details of it.

<%@ taglib prefix="s" uri="/struts-tags" %>
 
<html>
<head>
</head>
 
<body>
<h1>Struts 2 Iterator tag example</h1>

<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-2836379775501347"
     data-ad-slot="8821506761"
     data-ad-format="auto"
     data-ad-region="mkyongregion"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script><h2>Simple Iterator</h2>
<ol>
<s:iterator value="comboMeals">
  <li><s:property /></li>
</s:iterator>
</ol>

<h2>Iterator with IteratorStatus</h2>
<table>
<s:iterator value="comboMeals" status="comboMealsStatus">
  <tr>
  	<s:if test="#comboMealsStatus.even == true">
      <td style="background: #CCCCCC"><s:property/></td>
    </s:if>
    <s:elseif test="#comboMealsStatus.first == true">
      <td><s:property/> (This is first value) </td>
    </s:elseif>
    <s:else>
      <td><s:property/></td>
    </s:else>
  </tr>
</s:iterator>
</table>

</body>
</html>

3. struts.xml

Link it ~


<?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="default" namespace="/" extends="struts-default">
	
		<action name="iteratorKFCAction" 
			class="com.mkyong.common.action.IteratorKFCAction" >
			<result name="success">pages/iterator.jsp</result>
		</action>
		
	</package>
		
</struts>

4. Demo

http://localhost:8080/Struts2Example/iteratorKFCAction.action

Struts 2 Iterator tag

Reference

  1. Struts 2 Iterator tag example
  2. IteratorStatus documentation

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

Leave a Reply

avatar
newest oldest most voted
Pramod Kumar Sahani
Guest
Pramod Kumar Sahani

http://localhost/Struts2Example/iteratorKFCAction.action
use this link in your url bar it working
And that program is also working fine
there is no error

sasi
Guest
sasi

Hi sir, below equivalent tag in struts2? please guide me, like and ..etc in struts2

sasi
Guest
sasi
Shivu
Guest
Shivu

Hi,

I have a list of objects List, I can iterate simple fields such as name, dob, etc. but my Employee contains another object Address. I cant access fields from Address iterator.

Jethro
Guest
Jethro

this is not working for me..

Alexander
Guest
Alexander

Please explain me, you wrote this #comboMealsStatus.even == true. Why you added symbol # ?

Rajesh
Guest
Rajesh

This code is not working… just i got the following output:
Struts 2 Iterator tag example
Simple Iterator

Iterator with IteratorStatus

sachin
Guest
sachin

same here

ahmed
Guest
ahmed

Hi I have some issue working with nested struts iterator i m put my issue in this link
http://stackoverflow.com/questions/18789764/nested-iterator-struts2-global-variable

Ayia Napa_s 5 Best Hotels
Guest
Ayia Napa_s 5 Best Hotels

Hi there friends, how is all, and what you want to
say about this piece of writing, in my view its genuinely awesome designed for me.

Gaurav
Guest
Gaurav

Here is an extension to the above tutorial.
How to iterate over a List, Map and List using struts 2.
http://atechiediary.blogspot.com/2013/07/struts2-how-to-display-data-contained.html

Gaurav
Guest
Gaurav

It is actually List(of String), Map and List(of Objects)

nathan
Guest
nathan
Hello Mkyong, In the iterator.jsp page you did not add the struts form tag. that’s why the output not coming. please add this tag to the iterator.jsp page the below is the correct version <s:form action="iteratorKFCAction" > <h1>Struts 2 Iterator tag example</h1> <h3>Simple Iterator</h3> <ol> <s:iterator value="comboMeals"> <li><s:property /></li> </s:iterator> </ol> <h3>Iterator with IteratorStatus</h3> <table> <s:iterator value="comboMeals" status="comboMealsStatus"> <tr> <s:if test="#comboMealsStatus.even == true"> <td style="background: #CCCCCC"><s:property/></td> </s:if> <s:elseif test="#comboMealsStatus.first == true"> <td><s:property/> (This is first value) </td> </s:elseif> <s:else> <td><s:property/></td> </s:else> </tr> </s:iterator> </table> </s:form> now anyone can get the output, its working fine
nathan
Guest
nathan

hello MkYong

I go the below output only, What is the problem, why data not coming?

Struts 2 Iterator tag example
Simple Iterator
Iterator with IteratorStatus

mahmoud
Guest
mahmoud
hi, i m traying to iterat a lsit of object in my jsp i use this <s:iterator value="hotelfound" var="hot"> <tr> <td> <s:property value="%{hot.hoName}"/></td> </tr> </s:iterator> my model object contain other object public class Hotels { private String hotel_id; private String hoName; private String hoDEscrip; private HotelAdress ho_ddress; private Integer tel1; private Integer tel2; private String imgs; private String login; private String password; public String getLogin() { return login; } public void setLogin(String login) { this.login = login; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getHoName() { return hoName;… Read more »
Joseph Kingston Leo
Guest
Joseph Kingston Leo

Thank you it was very useful…………… Joseph (SCJP6)

Ravi Saini
Guest
Ravi Saini

Dear sir
i am using struts2 and i have a problem regarding to struts2

problem:-I got data from mysql database to html table using iterator tag
its working good for once.if i execute same action class again then repeated
data add in same table.plz give me solution

Jeetendra biswas
Guest
Jeetendra biswas

comboMeals.size();

Jeetendra biswas
Guest
Jeetendra biswas

this is not working. what is this non sense thing is that those are not working….. this is bullshit!!

gd
Guest
gd

Nonsense this is Working!..

skl
Guest
skl

I just get the below output..

Struts 2 Iterator tag example
Simple Iterator
Iterator with IteratorStatus

I am not getting any data ?? do you what the issue…
I just copy pasted the above example

Arun
Guest
Arun

hi mkyong !!

useful article..do u have any struts 1.2 jsp iterator tag example..as i m a beginner,i m little bit struggling in this combination ..thanks

Arun.

gd
Guest
gd

shutup !..

Oscar
Guest
Oscar

Very useful tuts!

But i would need the same from an javabean’s attribute (as list of course) implemeting the modeldriven?

Thks

trackback
Struts 2 Tutorials

[…] iterator tag example Struts 2 Iterator tag is used to iterate over a value, which can be any of java.util.Collection or java.util.Iterator. […]

khalil
Guest
khalil
Bonjour , cette classe afficher des champs enregistrer dans une table nommé utilisateur voici lecode de cette classe : public class ListUsersAction extends BaseAction { private List users; public String execute() throws TaskManagerException { users = DBUtils.getUserMgr().getAll(); return SUCCESS; } public List getUsers() { return users; } public void setUsers(List users) { this.users = users; } alors que je voudrai afficher juste la ligne des champs qui son id=66 , comment je peut le faire svp ?????
nathan
Guest
nathan

Struts 2 Iterator tag example
Simple Iterator
Iterator with IteratorStatus

Hello sir, I could not get the output, what is the problem