Struts 2 Iterator tag example

Source : http://www.mkyong.com/struts2/struts-2-iterator-tag-example/

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 comboMeals;

public Listlt;String> getComboMeals() {
return comboMeals;
}

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

public String execute() {

comboMeals = new ArrayList();
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








Struts 2 Iterator tag example



Simple Iterator








Iterator with IteratorStatus
















(This is first value)




Struts.xml


<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">







<action name="iteratorKFCAction"
class="com.mkyong.common.action.IteratorKFCAction" >
pages/iterator.jsp





About Vineet Verma

Developer/Blogger/Gamer/Lazy Couch Potato...:P Need PDF Books: Knowledge Base
This entry was posted in Struts 2. Bookmark the permalink.

2 Responses to Struts 2 Iterator tag example

  1. krishna says:

    is there any way to get value from status like

    plz help..

    this will help me iun nested iterators.

    Like

  2. Krishna, I can't see the text after like….
    Please replace < by <

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s