This patch adds a new attribute, 'indexId', to the <logic:iterate> tag which
is used to expose the current index, at each iteration, as a scripting
variable. The attribute names the desired scripting variable.

A couple of people expressed interest in this, and it turned out to be
straightforward, so I figured I'd post the patch.

--
Martin Cooper

Index: IterateTag.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-struts/src/share/org/apache/struts/taglib/logic/IterateTag.java,v
retrieving revision 1.11
diff -u -r1.11 IterateTag.java
--- IterateTag.java     2001/04/29 01:26:19     1.11
+++ IterateTag.java     2001/05/27 17:41:35
@@ -188,6 +188,20 @@
 
 
     /**
+     * The name of the scripting variable to be exposed as the current index.
+     */
+    protected String indexId = null;
+
+    public String getIndexId() {
+       return (this.indexId);
+    }
+
+    public void setIndexId(String indexId) {
+       this.indexId = indexId;
+    }
+
+
+    /**
      * The length value or attribute name (<=0 means no limit).
      */
     protected String length = null;
@@ -366,6 +380,8 @@
                 pageContext.setAttribute(id, element);
            lengthCount++;
             started = true;
+            if (indexId != null)
+                pageContext.setAttribute(indexId, new Integer(getIndex()));
            return (EVAL_BODY_TAG);
         } else
             return (SKIP_BODY);
@@ -397,6 +413,8 @@
             else
                 pageContext.setAttribute(id, element);
            lengthCount++;
+            if (indexId != null)
+                pageContext.setAttribute(indexId, new Integer(getIndex()));
            return (EVAL_BODY_TAG);
        } else
            return (SKIP_BODY);
Index: IterateTei.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-struts/src/share/org/apache/struts/taglib/logic/IterateTei.java,v
retrieving revision 1.4
diff -u -r1.4 IterateTei.java
--- IterateTei.java     2001/02/12 21:49:56     1.4
+++ IterateTei.java     2001/05/27 17:41:45
@@ -87,13 +87,26 @@
         String type = data.getAttributeString("type");
         if (type == null)
             type = "java.lang.Object";
+        VariableInfo typeInfo = new VariableInfo(
+                data.getAttributeString("id"),
+               type,
+               true,
+               VariableInfo.NESTED);
 
-       return new VariableInfo[] {
-         new VariableInfo(data.getAttributeString("id"),
-                          type,
-                          true,
-                          VariableInfo.NESTED)
-       };
+        String indexId = data.getAttributeString("indexId");
+        VariableInfo indexIdInfo = null;
+        if (indexId != null)
+            indexIdInfo = new VariableInfo(
+                indexId,
+               "java.lang.Integer",
+               true,
+               VariableInfo.NESTED);
+
+        if (indexIdInfo == null) {
+            return new VariableInfo[] { typeInfo };
+        } else {
+            return new VariableInfo[] { typeInfo, indexIdInfo };
+        }
 
     }
 
Index: struts-logic.xml
===================================================================
RCS file: /home/cvspublic/jakarta-struts/doc/struts-logic.xml,v
retrieving revision 1.6
diff -u -r1.6 struts-logic.xml
--- struts-logic.xml    2001/05/09 19:31:02     1.6
+++ struts-logic.xml    2001/05/27 17:42:22
@@ -465,6 +465,16 @@
     </attribute>
 
     <attribute>
+      <name>indexId</name>
+      <required>false</required>
+      <rtexprvalue>true</rtexprvalue>
+      <info>
+      <p>The name of a page scope JSP bean that will contain the current
+      index of the collection on each iteration.</p>
+      </info>
+    </attribute>
+
+    <attribute>
       <name>length</name>
       <required>false</required>
       <rtexprvalue>true</rtexprvalue>

Reply via email to