Struts 2 iterator control tag

s:iterator:

The iterator tag is used to iterate over a value. The value can be any of the java.util.Collection or java.util.Iterator.

Syntax: 

<s:iterator value="fieldValue">
        //do something
</s:iterator>

Note: The IteratorStatus class is used to get the iteration status details like count, even, odd etc. To use this functionality we have to declare the IteratorStatus class in the status attribute of the iterator tag.

Example:

index.jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  	<title>Struts 2 s:iterator control tag example</title>
	<META HTTP-EQUIV="Refresh" CONTENT="0;URL=Test.action">
  </head>  
  <body>
  </body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 
 
 	<filter>
	  <filter-name>struts2</filter-name>
	  <filter-class>
        	org.apache.struts2.dispatcher.ng.
        	filter.StrutsPrepareAndExecuteFilter
          </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>

struts.xml

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
 
<struts>
 
	<package name="default" extends="struts-default">
		<action name="Test" class="com.w3spoint.action.Test">
			<result name="success">/test.jsp</result>
		</action>
	</package>
 
</struts>

Test.java

import java.util.ArrayList;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
 
/**
 * This class is used as an action class.
 * @author w3spoint
 */
public class Test extends ActionSupport{
	//data members
	private List<String> subjectList;
 
	//business logic
	public String execute(){
		subjectList = new ArrayList<String>();
		subjectList.add("java");
		subjectList.add("DBMS");
		subjectList.add("Networing");
		subjectList.add("Compiler");
 
		return SUCCESS;	
	}
 
	//getter setters
	public List<String> getSubjectList() {
		return subjectList;
	}
 
	public void setSubjectList(List<String> subjectList) {
		this.subjectList = subjectList;
	}
 
}

welcome.jsp

<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
	<head>
		<title>Struts 2 s:iterator control tag example</title>
	</head>
	<body>
		<h3>This is a s:iterator control tag example.</h3>
 
		<h4>Simple iterator</h4>
		<s:iterator value="subjectList">
			<s:property/><br/>
		</s:iterator>
 
		<h4>Iterator with iteration details</h4>
	      <s:iterator value="subjectList" status="subjectListStatus">
			<s:if test="#subjectListStatus.first == true">
				First Value: <s:property/>
			</s:if>
			<s:elseif test="#subjectListStatus.last == true">
      			                Last Value: <s:property/>
    		               </s:elseif>
    		               <s:else>
    			                <s:property/>
    		               </s:else>
    		               <br/>
		</s:iterator>
 
	</body>
</html>

Output:

struts 43   Download this example.   Next Topic: Struts 2 merge control tag with example. Previous Topic: Struts 2 If, else if and else control tag with example.

 

Please follow and like us:
Content Protection by DMCA.com