Struts <logic:iterate> Tag

The <logic:iterate> tag is used to iterate the data or object over collection. In order to use the Struts HTML Tags you need to include the following taglib directive in the jsp page.

<%@taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> 

First create a new Dynamic Web Project and configure it as Maven Project. For Reference, Click Here

Add the following dependencies in pom.xml


1. Create DynaActionForm

First we create an entry for DynaActionForm in struts-config.xml

    <form-bean name="EmployeeForm"  
        <form-property name="employeeId" type="java.lang.Integer" />
         <form-property name="employeeName" type="java.lang.String" />
         <form-property name="employeeList" type="java.util.ArrayList" />

2. Model Class

Create a Employee class and write the following code.

package com.kruders.domain;
public class Employee {
    private Integer employeeId;
    private String employeeName;
    public Employee(){
    public Employee(Integer employeeId, String employeeName){
        this.employeeId = employeeId;
        this.employeeName = employeeName;
    public Integer getEmployeeId() {
        return employeeId;
    public void setEmployeeId(Integer employeeId) {
        this.employeeId = employeeId;
    public String getEmployeeName() {
        return employeeName;
    public void setEmployeeName(String employeeName) {
        this.employeeName = employeeName;

3. Action Class (Controller)

Let us create an Action class that will handle the request. Here Action Class contains the business logic.

package com.kruders.action;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import com.kruders.domain.Employee;
public class EmployeeAction extends Action{
    public ActionForward execute(ActionMapping mapping,ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
                throws Exception {
        DynaActionForm EmployeeForm = (DynaActionForm)form;
        ArrayList employeeList = new ArrayList();
        employeeList.add(new Employee(1, "Puneet Aggarwal"));
        employeeList.add(new Employee(2, "Sahil Verma"));
        EmployeeForm.set("employeeList", employeeList);
        return mapping.findForward("success");

4. View

Create welcome.jsp and write the following code.

<%@taglib uri="" prefix="html"%>
<title>Welcome Page</title>
<html:form action="/iterate">
    <<html:submit>Click Here</html:submit> to retrieve details using <logic:iterate> Tag

Now create another jsp file iterate.jsp that contains the struts logic tag to iterate the data.

<%@taglib uri="" prefix="html"%>
<%@taglib uri="" prefix="logic"%>
<%@taglib uri="" prefix="bean"%>
<title>Struts Logic Iterate Tag <logic:iterate></title>
    Employee Details
    <logic:iterate id="employee" name="EmployeeForm" property="employeeList">
       <br><bean:write name="employee" property="employeeName" />

5. Struts Config

Add the following code in struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC 
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" 
       <form-bean name="EmployeeForm" type="org.apache.struts.action.DynaActionForm">
         <form-property name="employeeId" type="java.lang.Integer" />
         <form-property name="employeeName" type="java.lang.String" />
         <form-property name="employeeList" type="java.util.ArrayList" />
        <forward name="iterate" path="/"/>
        <action input="/iterate.jsp" name="EmployeeForm" path="/iterate"  validate="true" scope="request" type="com.kruders.action.EmployeeAction">
            <forward name="success" path="/iterate.jsp" />

6. Deployment Descriptor

Now configure the deployment descriptor. Here, we have asked the container to give our ActionServlet any request that matches the pattern *.do

Add the following configuration information in the web.xml file


7. Run Project

Now when you run the project, following screen will be displayed as in Figure 31.1

Figure 31.1 Figure 31.1

Now when you click submit button on welcome page, following screen will be displayed as shown in Figure 31.2

Figure 31.2 Figure 31.2

The folder structure of the example is shown below in Figure 31.3

Figure 31.3 Figure 31.3

You can download the source code of this example here.


No comments yet.

Leave a Reply