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 ControlTagAction.java 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>();
  userList.add("Dinesh");
  userList.add("Sweety");
  userList.add("Aadesh");
  userList.add("Vinesh");
  userList.add("Sandhya");
  userList.add("Rajput");
  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" %>

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

Configuration Files
Your struts.xml should look like:

<?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.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>
        </action>
        
    </package>
 </struts>

Your web.xml should look like:

<?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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>Struts2ControlTags</display-name>
  <filter>
        <filter-name>struts2</filter-name>
        <filter-class>
            org.apache.struts2.dispatcher.FilterDispatcher
        </filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

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
Struts2ITERATOR_Tag.zip

 

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