top of page
  • Writer's pictureOrgLance Technologies LLP

Exploring the <s:iterator> Tag in Apache Struts 2



Apache Struts 2 is a popular framework for building Java web applications. It provides a variety of tags to simplify the development of dynamic web pages. One of these tags is the <s:iterator> tag, which is essential for iterating over collections such as lists, arrays, or maps. This article delves into the usage of the <s:iterator> tag with various examples to demonstrate its versatility.

Overview of <s:iterator> Tag

The <s:iterator> tag is used to iterate over a collection of items. Each item in the collection can be processed individually within the body of the tag. It is analogous to a for-each loop in Java. The tag is highly useful when dealing with dynamic data in JSP pages.

Basic Syntax

The basic syntax of the <s:iterator> tag is as follows:

<s:iterator value="collection"> 
	<!-- Body content to process each item --> 
</s:iterator>

Here, value is the attribute that specifies the collection to iterate over. The collection can be a list, array, map, or any other iterable object.

Examples

Let's explore some practical examples of using the <s:iterator> tag.

Example 1: Iterating Over a List

Suppose we have a list of strings representing names. We want to display each name in an unordered list.

Action Class:

import com.opensymphony.xwork2.ActionSupport;
import java.util.Arrays;
import java.util.List;

public class NamesAction extends ActionSupport {
    private List<String> names;

    public String execute() {
        names = Arrays.asList("Alice", "Bob", "Charlie", "David");
        return SUCCESS;
    }

    public List<String> getNames() {
        return names;
    }
}

JSP Page:

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
    <title>Names List</title>
</head>
<body>
    <h2>Names List</h2>
    <ul>
        <s:iterator value="names">
            <li><s:property /></li>
        </s:iterator>
    </ul>
</body>
</html>

In this example, the <s:iterator> tag iterates over the names list and the <s:property /> tag prints each name inside an <li> element.

Example 2: Iterating Over a Map

Let's consider iterating over a map where we display key-value pairs.

Action Class:

import com.opensymphony.xwork2.ActionSupport;
import java.util.HashMap;
import java.util.Map;

public class ProductsAction extends ActionSupport {
    private Map<String, Double> products;

    public String execute() {
        products = new HashMap<>();
        products.put("Laptop", 999.99);
        products.put("Tablet", 499.99);
        products.put("Smartphone", 799.99);
        return SUCCESS;
    }

    public Map<String, Double> getProducts() {
        return products;
    }
}

JSP Page:

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
    <title>Products List</title>
</head>
<body>
    <h2>Products List</h2>
    <table border="1">
        <tr>
            <th>Product</th>
            <th>Price</th>
        </tr>
        <s:iterator value="products">
            <tr>
                <td><s:property value="key" /></td>
                <td><s:property value="value" /></td>
            </tr>
        </s:iterator>
    </table>
</body>
</html>

In this example, the <s:iterator> tag iterates over the products map. The key and value properties are used to display each key-value pair in a table.

Example 3: Nested Iteration

Sometimes, you may need to perform nested iterations. For instance, consider a scenario where each product has a list of features.

Action Class:

import com.opensymphony.xwork2.ActionSupport;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ProductFeaturesAction extends ActionSupport {
    private Map<String, List<String>> productFeatures;

    public String execute() {
        productFeatures = new HashMap<>();
        productFeatures.put("Laptop", Arrays.asList("Intel i7", "16GB RAM", "512GB SSD"));
        productFeatures.put("Tablet", Arrays.asList("10-inch screen", "4GB RAM", "64GB storage"));
        productFeatures.put("Smartphone", Arrays.asList("5G", "128GB storage", "Triple Camera"));
        return SUCCESS;
    }

    public Map<String, List<String>> getProductFeatures() {
        return productFeatures;
    }
}

JSP Page:

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
    <title>Product Features</title>
</head>
<body>
    <h2>Product Features</h2>
    <s:iterator value="productFeatures">
        <h3><s:property value="key" /></h3>
        <ul>
            <s:iterator value="value">
                <li><s:property /></li>
            </s:iterator>
        </ul>
    </s:iterator>
</body>
</html>

In this nested iteration example, the outer <s:iterator> tag iterates over the productFeatures map, and the inner <s:iterator> tag iterates over the list of features for each product.

Example 4: Using status Attribute

The status attribute can be used to track the state of the iteration. It provides access to the IteratorStatus object, which contains useful information such as the current index, whether the current item is the first or last, etc.

Action Class:

import com.opensymphony.xwork2.ActionSupport;
import java.util.Arrays;
import java.util.List;

public class ColorsAction extends ActionSupport {
    private List<String> colors;

    public String execute() {
        colors = Arrays.asList("Red", "Green", "Blue", "Yellow");
        return SUCCESS;
    }

    public List<String> getColors() {
        return colors;
    }
}

JSP Page:

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
    <title>Colors List</title>
</head>
<body>
    <h2>Colors List</h2>
    <ol>
        <s:iterator value="colors" status="colorStatus">
            <li>
                <s:property /> - Index: <s:property value="#colorStatus.index" />
                <s:if test="#colorStatus.first"> (First)</s:if>
                <s:if test="#colorStatus.last"> (Last)</s:if>
            </li>
        </s:iterator>
    </ol>
</body>
</html>

In this example, the status attribute is used to track the iteration status. The #colorStatus variable provides access to properties like index, first, and last.


Conclusion

The <s:iterator> tag in Struts 2 is a powerful tool for iterating over collections in JSP pages. It simplifies the process of dynamically displaying data from lists, maps, and other iterable objects. By utilizing various features such as nested iterations and the status attribute, developers can create dynamic and data-driven web applications efficiently.


For more detailed information about the <s:iterator> tag and other Struts 2 tags, refer to the official Struts 2 documentation.

8 views0 comments

Recent Posts

See All

Comments


bottom of page