Struts 2 Iterator Tag Example

In this section, we are going to describe the Iterator tag. Iterator tag is used to iterate over a value. An iterable value can be either of: java.util.Collection, java.util.Iterator.
Create action classes:
First of all let us create a simple class called which looks like:
package com.dineshonjava.struts2.action;
import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;

 * @author Dinesh Rajput
public class ControlTagAction extends ActionSupport{

 private static final long serialVersionUID = 1L;
 private List<String> userList;
 public String execute() {
  userList = new ArrayList<String>();
  return SUCCESS;

 public List<String> getUserList() {
  return userList;

 public void setUserList(List<String> userList) {
  this.userList = userList;

Create views
Create a file called users.jsp with the following contents:
<%@ taglib prefix="s" uri="/struts-tags" %>

  <title>Iterator Tag Example!</title>
  <h1><span style="background-color: #FFFFcc">Iterator Tag Example!</span></h1>
  <s:iterator value="userList">
  <s:property /><br>

Configuration Files
Your struts.xml should look like:
<?xml version="1.0" encoding="UTF-8" ?>
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="false" />
    <constant name="struts.custom.i18n.resources" value="myapp" />
    <package name="default" extends="struts-default" namespace="/">
        <action name="users" class="com.dineshonjava.struts2.action.ControlTagAction">
            <result name="success">/users.jsp</result>
Your web.xml should look like:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" xmlns="" xmlns:web="" xsi:schemaLocation="" id="WebApp_ID" version="3.0">
Right click on the project name and click Export > WAR File to create a War file. Then deploy this WAR in the Tomcat's webapps directory. Finally, start Tomcat server and try to access
URL http://localhost:8080/doj/users.
This will give you following screen:

Download Source Code

<<Previous <<   || Index ||   >>Next >>

No comments:

Post a Comment