You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@struts.apache.org by pb...@apache.org on 2008/06/06 08:19:40 UTC

svn commit: r663836 - in /struts/struts1/trunk/taglib/src/main: java/org/apache/struts/taglib/html/MessagesTag.java java/org/apache/struts/taglib/logic/MessagesPresentTag.java resources/META-INF/tld/struts-html.tld resources/META-INF/tld/struts-logic.tld

Author: pbenedict
Date: Thu Jun  5 23:19:40 2008
New Revision: 663836

URL: http://svn.apache.org/viewvc?rev=663836&view=rev
Log:
STR-2913: Expose count of messages as page attribute

Modified:
    struts/struts1/trunk/taglib/src/main/java/org/apache/struts/taglib/html/MessagesTag.java
    struts/struts1/trunk/taglib/src/main/java/org/apache/struts/taglib/logic/MessagesPresentTag.java
    struts/struts1/trunk/taglib/src/main/resources/META-INF/tld/struts-html.tld
    struts/struts1/trunk/taglib/src/main/resources/META-INF/tld/struts-logic.tld

Modified: struts/struts1/trunk/taglib/src/main/java/org/apache/struts/taglib/html/MessagesTag.java
URL: http://svn.apache.org/viewvc/struts/struts1/trunk/taglib/src/main/java/org/apache/struts/taglib/html/MessagesTag.java?rev=663836&r1=663835&r2=663836&view=diff
==============================================================================
--- struts/struts1/trunk/taglib/src/main/java/org/apache/struts/taglib/html/MessagesTag.java (original)
+++ struts/struts1/trunk/taglib/src/main/java/org/apache/struts/taglib/html/MessagesTag.java Thu Jun  5 23:19:40 2008
@@ -102,6 +102,12 @@
      * will be used to retrieve the messages from scope.
      */
     protected String message = null;
+    
+    /**
+     * The name of the page-scoped attribute to be populated
+     * with the message count of the specifie dproperty.
+     */
+    protected String count;
 
     /**
      * Filter the message replacement values for characters that are 
@@ -180,6 +186,14 @@
     public void setFilterArgs(boolean filterArgs) {
         this.filterArgs = filterArgs;
     }
+    
+    public void setCount(String count) {
+    	this.count = count;
+    }
+    
+    public String getCount() {
+    	return count;
+    }
 
     /**
      * Construct an iterator for the specified collection, and begin looping
@@ -210,9 +224,20 @@
         }
 
         // Acquire the collection we are going to iterate over
-        this.iterator =
-            (property == null) ? messages.get() : messages.get(property);
-
+        int size;
+        if (property == null) {
+        	this.iterator = messages.get();
+        	size = messages.size();
+        } else {
+        	this.iterator = messages.get(property);
+        	size = messages.size(property);
+        }
+        
+        // Expose the count when specified
+        if (count != null) {
+        	pageContext.setAttribute(count, new Integer(size));
+        }
+        
         // Store the first value and evaluate, or skip the body if none
         if (!this.iterator.hasNext()) {
             return SKIP_BODY;
@@ -335,6 +360,10 @@
             }
         }
 
+    	if (count != null) {
+            pageContext.removeAttribute(count);
+    	}
+
         return EVAL_PAGE;
     }
 
@@ -354,5 +383,6 @@
         footer = null;
         message = null;
         filterArgs = false;
+        count = null;
     }
 }

Modified: struts/struts1/trunk/taglib/src/main/java/org/apache/struts/taglib/logic/MessagesPresentTag.java
URL: http://svn.apache.org/viewvc/struts/struts1/trunk/taglib/src/main/java/org/apache/struts/taglib/logic/MessagesPresentTag.java?rev=663836&r1=663835&r2=663836&view=diff
==============================================================================
--- struts/struts1/trunk/taglib/src/main/java/org/apache/struts/taglib/logic/MessagesPresentTag.java (original)
+++ struts/struts1/trunk/taglib/src/main/java/org/apache/struts/taglib/logic/MessagesPresentTag.java Thu Jun  5 23:19:40 2008
@@ -39,12 +39,19 @@
  * @since Struts 1.1
  */
 public class MessagesPresentTag extends ConditionalTagBase {
-    /**
+
+	/**
      * If this is set to 'true', then the <code>Globals.MESSAGE_KEY</code>
      * will be used to retrieve the messages from scope.
      */
     protected String message = null;
 
+    /**
+     * The name of the page-scoped attribute to be populated
+     * with the message count of the specifie dproperty.
+     */
+    protected String count;
+
     public MessagesPresentTag() {
         name = Globals.ERROR_KEY;
     }
@@ -57,6 +64,14 @@
         this.message = message;
     }
 
+    public void setCount(String count) {
+    	this.count = count;
+    }
+    
+    public String getCount() {
+    	return count;
+    }
+
     /**
      * Evaluate the condition that is being tested by this particular tag, and
      * return <code>true</code> if the nested body content of this tag should
@@ -94,11 +109,32 @@
             throw e;
         }
 
-        Iterator iterator = (property == null) ? am.get() : am.get(property);
+        // Acquire the collection of messages
+        Iterator iterator;
+        int size;
+        if (property == null) {
+        	iterator = am.get();
+        	size = am.size();
+        } else {
+            iterator = am.get(property); 
+            size = am.size(property);
+        }
 
+        // Expose the count when specified
+        if (count != null) {
+        	pageContext.setAttribute(count, new Integer(size));
+        }
+        
         return (iterator.hasNext() == desired);
     }
 
+    public int doEndTag() throws JspException {
+    	if (count != null) {
+            pageContext.removeAttribute(count);
+    	}
+    	return super.doEndTag();
+    }
+    
     /**
      * Release all allocated resources.
      */
@@ -106,5 +142,6 @@
         super.release();
         name = Globals.ERROR_KEY;
         message = null;
+        count = null;
     }
 }

Modified: struts/struts1/trunk/taglib/src/main/resources/META-INF/tld/struts-html.tld
URL: http://svn.apache.org/viewvc/struts/struts1/trunk/taglib/src/main/resources/META-INF/tld/struts-html.tld?rev=663836&r1=663835&r2=663836&view=diff
==============================================================================
--- struts/struts1/trunk/taglib/src/main/resources/META-INF/tld/struts-html.tld (original)
+++ struts/struts1/trunk/taglib/src/main/resources/META-INF/tld/struts-html.tld Thu Jun  5 23:19:40 2008
@@ -5447,6 +5447,23 @@
                   ]]>
             </description>
         </attribute>
+        <attribute>
+            <name>count</name>
+            <required>false</required>
+            <rtexprvalue>true</rtexprvalue>
+            <description>
+                <![CDATA[
+                Specifies the name of the page-scoped attribute to set 
+                with the message count for the specified property. This 
+                is useful, for instance, when needing to display the number 
+                of field errors produced by a form submission.
+                The attribute goes out of scope after the tag executes.
+        
+                <dl><dt><b>Since:</b></dt>
+                <dd>Struts 1.4</dd></dl>
+                ]]>
+            </description>
+        </attribute>
     </tag>
     <tag>
         <name>multibox</name>

Modified: struts/struts1/trunk/taglib/src/main/resources/META-INF/tld/struts-logic.tld
URL: http://svn.apache.org/viewvc/struts/struts1/trunk/taglib/src/main/resources/META-INF/tld/struts-logic.tld?rev=663836&r1=663835&r2=663836&view=diff
==============================================================================
--- struts/struts1/trunk/taglib/src/main/resources/META-INF/tld/struts-logic.tld (original)
+++ struts/struts1/trunk/taglib/src/main/resources/META-INF/tld/struts-logic.tld Thu Jun  5 23:19:40 2008
@@ -1096,6 +1096,23 @@
       ]]>
             </description>
         </attribute>
+        <attribute>
+            <name>count</name>
+            <required>false</required>
+            <rtexprvalue>true</rtexprvalue>
+            <description>
+                <![CDATA[
+                Specifies the name of the page-scoped attribute to set 
+                with the message count. This is useful, for instance, when 
+                needing to total the number of messages or errors produced 
+                by a form submission. The attribute goes out of scope after 
+                the tag executes.
+        
+                <dl><dt><b>Since:</b></dt>
+                <dd>Struts 1.4</dd></dl>
+                ]]>
+            </description>
+        </attribute>
     </tag>
     <tag>
         <name>notEmpty</name>