Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction 
1.1.
Strut 2
2.
Iterator Tags
3.
Iterator Tag Example
3.1.
index.jsp
3.2.
Web.xml
3.3.
Test.java
3.4.
Welcome.jsp
3.5.
Output
4.
Frequently Asked Questions
4.1.
What is struct2?
4.2.
In the Struts2 framework, what is a model?
4.3.
What do HTML struts do?
4.4.
What are Iterator Tags?
4.5.
Are Struts still in use?
5.
Conclusion
Last Updated: Mar 27, 2024
Medium

The Iterator Tags

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction 

You may be wondering what Iterator tags are but before going into that, let us first look into where they came from.

introductory image

Strut 2

Java-based apps are typically made using Struts. It is the entire framework for tasks, including development, deployment, and maintenance. The MVC (Model View Controller)-the based framework is called Struts.

Iterator tags come under Control tags in Strut.

Iterator Tags

The Iterator tag is used in the struts web application to iterate through any collection and output any values found within. Each element in the collection is repeated through by the iterator tag, which prints the importance of each component.

 The value can be any of the java.util.Collection or java.util.Iterator.

classification image

Syntax:

<s:iterator value="fieldValue">
        //write somtehing
</s:iterator>

Note: To obtain information about the iteration status, such as count, even, or odd, use the IteratorStatus class. The IteratorStatus class must be declared in the status attribute of the iterator tag to access this capability.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Iterator Tag Example

index.jsp

<!DOCTYPE HTML PUBLIC "-//NINJAS HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>Struts 2 s: Iterator Tag Example</title>
    <META HTTP-EQUIV="Refresh" CONTENT="0;URL=Test.action">
  </head>  
  <body>
  </body>
</html>

Web.xml

xml image

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    xmlns:xsi="http://www.coding_ninjas.org/2001/XMLSchema-instance"
    
   
    <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.coding_ninjas.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 employed as a class for actions. */
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("Networking");
        subjectList.add("Compiler");
 
        return SUCCESS;
    }
 
    // Getter 
    public List<String> getSubjectList() {
        return subjectList;
    }
 
    public void setSubjectList(List<String> subjectList) {
        this.subjectList = subjectList;
    }
 
}

Welcome.jsp

jsp image 

<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
    <head>
        <title>Struts 2 s:iterator control tag example</title>
    </head>
    <body>
        <h3>Example of s:iterator tag.</h3>
 
        <h4>Ususal iterator</h4>
        <s:iterator value="subjectList">
            <s:property/><br/>
        </s:iterator>
 
        <h4>Iterator providing iteration information</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

output image

 

Must Read Apache Server

Frequently Asked Questions

What is struct2?

A well-liked and established web application framework based on the MVC design paradigm is Struts2. The Struts architecture has been completely rewritten in Struts2, which is more than just a new version of Struts 1.

In the Struts2 framework, what is a model?

The part in charge of managing the system's data can be found in any MVC framework model. It is not necessary for MVC to contain the model itself; alternatively, it may contain connections to one or more Models.

What do HTML struts do?

The Struts HTML Tags drawer allows you to build Struts HTML tags. These tags, along with others that are usually helpful in the building of HTML-based user interfaces, are used to create Struts input forms.

What are Iterator Tags?

Any collection can be iterated through to print values using the Iterator tag. Each element in the collection is iterated through by the iterator tag, which prints the values for each element.

Are Struts still in use?

According to estimates, at least 65 percent of the Fortune 100 firms rely on web applications created with the Apache Struts framework in 2017 despite the Apache Struts project being on the market for 18 years.

Conclusion

In this blog, we have learned about the struts and iterator tag in brief.

To know more about the struts and other statements Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in pygameCompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But suppose you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc. In that case, you must look at the problems, interview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

closure image
Previous article
The If and Else Tags
Next article
The Merge Tag
Live masterclass