Back to: Java Struts Tutorials
Struts 2 Iterator Tag
In this article, I am going to discuss Struts 2 Iterator Tag with Examples. Please read our previous article where we discussed Struts 2 If Else Tag.
What is Iterator Tag in Struts 2?
The <s:iterator> tag in Struts 2 is used to iterate over a collection, such as a List, Map, or array, and renders its body for each element in the collection.
How to Implement Iterator Tags in Struts 2?
Step 1: Create a new maven project in Eclipse. We will use Maven to manage the dependencies of struts2. This way, we do not have to import JARs.
Remember to set the packaging to war. This is because we shall be deploying a web application.
Finally, click on the finish button to create the project. The following project directories need to be created:
Step 2: Modify pom.xml to add the required dependencies and the build configuration:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.dotnet</groupId> <artifactId>strutex</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <dependencies> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>6.1.2.1</version> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts-taglib</artifactId> <version>1.3.10</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.3</version> </dependency> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> <dependency> <groupId>javax.servlet.jsp.jstl</groupId> <artifactId>jstl-api</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>2.3.3</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-convention-plugin</artifactId> <version>6.2.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.11.0</version> <configuration> <source>17</source> <target>17</target> </configuration> </plugin> </plugins> </build> </project>
Step 3: In the src/main/webapp directory, create a new file called index.jsp. Add the following content to the file:
<%@ page contentType = "text/html; charset = UTF-8" %> <%@ taglib prefix = "s" uri = "/struts-tags" %> <html> <head> <title>Students</title> </head> <body> <b>Example of Iterator Tag</b><br/> <s:iterator value = "students"> <s:property value = "name"/> , <s:property value = "faculty"/><br/> </s:iterator> <br/><br/> <b>Students sorted by Faculty</b> <br/> <s:bean name = "com.dotnet.FacultyComparator" var = "facultyComporator" /> <s:sort comparator = "facultyComporator" source = "students"> <s:iterator> <s:property value = "name"/> , <s:property value = "faculty"/><br/> </s:iterator> </s:sort> <br/><br/> <b>SubSet Tag - Students in Science faculty </b><br/> <s:subset decider="getScienceStudents" source = "students"> <s:iterator> <s:property value = "name"/> , <s:property value = "faculty"/><br/> </s:iterator> </s:subset> </body> </html>
Step 4: In the src/main/java directory, create a new file called Student.java. The file contains the Java action code. Add the following content to the file:
package com.dotnet; import java.util.ArrayList; import java.util.List; import org.apache.struts2.util.SubsetIteratorFilter.Decider; import com.opensymphony.xwork2.ActionSupport; public class Student extends ActionSupport { private String name, faculty; private List<Student> students; public Student() {} public Student(String name, String faculty) { this.name = name; this.faculty = faculty; } public String execute() { students = new ArrayList<Student>(); students.add(new Student("Adams Baker", "Science")); students.add(new Student("Clark Davis", "Commerce")); students.add(new Student("Evans Frank", "Arts")); return SUCCESS; } public Decider getScienceStudents () { return new Decider() { @Override public boolean decide(Object element) throws Exception { Student s = (Student) element; return s.getFaculty().equalsIgnoreCase("science"); } }; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getFaculty() { return faculty; } public void setFaculty(String faculty) { this.faculty = faculty; } public List<Student> getStudents() { return students; } public void setStudents(List<Student> students) { this.students = students; } }
Step 5: In the src/main/java directory, create a new file called FacultyComparator.java. The file contains the Java code to arrange the students by faculty. Add the following content to the file:
package com.dotnet; import java.util.Comparator; public class FacultyComporator implements Comparator<Student> { @Override public int compare(Student o1, Student o2) { return o1.getFaculty().compareTo(o2.getFaculty()); } }
Step 6: In the src/main/webapp directory, create a new file called struts.xml. This file is responsible for configuring struts2. Add the following content to the file:
<?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 = "helloworld" extends = "struts-default"> <action name = "Student" class = "com.dotnet.Student" method = "execute"> <result name = "success">/index.jsp</result> </action> </package> </struts>
Step 7: In the src/main/webapp directory, create a new directory called WEB-INF. In this directory, create a new file called web.xml. This file is responsible for implementing struts2 in the web application. Add the following content to the file:
<?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" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/web-app_3_0.xsd" id="apache-struts-config-example" version="3.0"> <display-name>Struts Config XML</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
Step 8: Compile and execute the program on the Tomcat server. The following page must open in your default web browser:
Congratulations! You now know how to implement iterator tags in struts2!
In the next article, I am going to discuss Struts 2 Merge and Append Tag with Examples. Here, in this article, I try to explain Struts 2 Iterator Tag with Examples and I hope you enjoy this Struts 2 Iterator Tag article.
Registration Open For New Online Training
Enhance Your Professional Journey with Our Upcoming Live Session. For complete information on Registration, Course Details, Syllabus, and to get the Zoom Credentials to attend the free live Demo Sessions, please click on the below links.