You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@click.apache.org by sa...@apache.org on 2009/05/28 19:49:50 UTC

svn commit: r779697 - in /incubator/click/trunk/click: extras/src/org/apache/click/extras/control/ framework/src/org/apache/click/ framework/src/org/apache/click/control/

Author: sabob
Date: Thu May 28 17:49:50 2009
New Revision: 779697

URL: http://svn.apache.org/viewvc?rev=779697&view=rev
Log:
added multivalued parameter support. CLK-554

Modified:
    incubator/click/trunk/click/extras/src/org/apache/click/extras/control/ExternalLink.java
    incubator/click/trunk/click/extras/src/org/apache/click/extras/control/SubmitLink.java
    incubator/click/trunk/click/framework/src/org/apache/click/Page.java
    incubator/click/trunk/click/framework/src/org/apache/click/control/AbstractLink.java
    incubator/click/trunk/click/framework/src/org/apache/click/control/ActionLink.java
    incubator/click/trunk/click/framework/src/org/apache/click/control/PageLink.java

Modified: incubator/click/trunk/click/extras/src/org/apache/click/extras/control/ExternalLink.java
URL: http://svn.apache.org/viewvc/incubator/click/trunk/click/extras/src/org/apache/click/extras/control/ExternalLink.java?rev=779697&r1=779696&r2=779697&view=diff
==============================================================================
--- incubator/click/trunk/click/extras/src/org/apache/click/extras/control/ExternalLink.java (original)
+++ incubator/click/trunk/click/extras/src/org/apache/click/extras/control/ExternalLink.java Thu May 28 17:49:50 2009
@@ -18,12 +18,8 @@
  */
 package org.apache.click.extras.control;
 
-import java.util.Iterator;
-import java.util.Map;
-
 import org.apache.click.ActionListener;
 import org.apache.click.control.AbstractLink;
-import org.apache.click.util.ClickUtils;
 import org.apache.click.util.HtmlStringBuffer;
 
 /**
@@ -110,19 +106,7 @@
         if (hasParameters()) {
             buffer.append("?");
 
-            Iterator i = getParameters().entrySet().iterator();
-            while (i.hasNext()) {
-                Map.Entry entry = (Map.Entry) i.next();
-                String name = entry.getKey().toString();
-                String value = entry.getValue().toString();
-
-                buffer.append(name);
-                buffer.append("=");
-                buffer.append(ClickUtils.encodeUrl(value, getContext()));
-                if (i.hasNext()) {
-                    buffer.append("&");
-                }
-            }
+            renderParameters(buffer, getParameters(), getContext());
         }
 
         return buffer.toString();

Modified: incubator/click/trunk/click/extras/src/org/apache/click/extras/control/SubmitLink.java
URL: http://svn.apache.org/viewvc/incubator/click/trunk/click/extras/src/org/apache/click/extras/control/SubmitLink.java?rev=779697&r1=779696&r2=779697&view=diff
==============================================================================
--- incubator/click/trunk/click/extras/src/org/apache/click/extras/control/SubmitLink.java (original)
+++ incubator/click/trunk/click/extras/src/org/apache/click/extras/control/SubmitLink.java Thu May 28 17:49:50 2009
@@ -268,80 +268,6 @@
     }
 
     /**
-     * @see org.apache.click.control.AbstractLink#getParameter(java.lang.String)
-     *
-     * @param name the name of request parameter
-     * @return the link request parameter value
-     */
-    public String getParameter(String name) {
-        if (hasParameters()) {
-            Object value = getParameters().get(name);
-
-            if (value instanceof String) {
-                return (String) value;
-            }
-
-            if (value instanceof String[]) {
-                String[] array = (String[]) value;
-                if (array.length >= 1) {
-                    return array[0];
-                } else {
-                    return null;
-                }
-            }
-
-            return (value == null ? null : value.toString());
-        } else {
-            return null;
-        }
-    }
-
-    /**
-     * Return the link request parameter values for the given name, or null if
-     * the parameter values does not exist.
-     *
-     * @param name the name of request parameter
-     * @return the link request parameter values
-     */
-    public String[] getParameterValues(String name) {
-        if (hasParameters()) {
-            Object values = getParameters().get(name);
-            if (values instanceof String) {
-                return new String[] { values.toString() };
-            }
-            if (values instanceof String[]) {
-                return (String[]) values;
-            } else {
-                return null;
-            }
-        } else {
-            return null;
-        }
-    }
-
-    /**
-     * Set the link parameter with the given parameter name and values. If the
-     * values are null, the parameter will be removed from the {@link #parameters}.
-     *
-     * @see org.apache.click.control.AbstractLink#setParameter(java.lang.String, java.lang.String)
-     *
-     * @param name the attribute name
-     * @param values the attribute values
-     * @throws IllegalArgumentException if name parameter is null
-     */
-    public void setParameterValues(String name, String[] values) {
-        if (name == null) {
-            throw new IllegalArgumentException("Null name parameter");
-        }
-
-        if (values != null) {
-            getParameters().put(name, values);
-        } else {
-            getParameters().remove(name);
-        }
-    }
-
-    /**
      * Set the paramter prefix that is applied to the SubmitLink parameters.
      *
      * @param prefix the parameter prefix
@@ -579,6 +505,11 @@
     private void renderParameter(String name, Object value,
         HtmlStringBuffer buffer, Context context) {
 
+        // Don't render null values
+        if (value == null) {
+            return;
+        }
+
         String encodedValue = ClickUtils.encodeUrl(value, context);
         buffer.append("&");
         if (hasParentForm()) {

Modified: incubator/click/trunk/click/framework/src/org/apache/click/Page.java
URL: http://svn.apache.org/viewvc/incubator/click/trunk/click/framework/src/org/apache/click/Page.java?rev=779697&r1=779696&r2=779697&view=diff
==============================================================================
--- incubator/click/trunk/click/framework/src/org/apache/click/Page.java (original)
+++ incubator/click/trunk/click/framework/src/org/apache/click/Page.java Thu May 28 17:49:50 2009
@@ -25,6 +25,7 @@
 import java.util.List;
 import java.util.Map;
 
+import org.apache.click.util.ClickUtils;
 import org.apache.click.util.Format;
 import org.apache.click.util.HtmlStringBuffer;
 import org.apache.click.util.MessagesMap;
@@ -1092,7 +1093,8 @@
      * <b>Please note</b> that Click will url encode the location by invoking
      * <tt>response.encodeRedirectURL(location)</tt> before redirecting.
      * <p/>
-     * See also {@link #setForward(String)}, {@link #setPath(String)}
+     * See also {@link #setRedirect(java.lang.String, java.util.Map)},
+     * {@link #setForward(String)}, {@link #setPath(String)}
      *
      * @param location the path to redirect the request to
      */
@@ -1118,7 +1120,38 @@
      * the map of request parameters to the location URL.
      * <p/>
      * The map keys will be used as the request parameter names and the map
-     * values will be used as the request parameter values.
+     * values will be used as the request parameter values. For example:
+     *
+     * <pre class="prettyprint">
+     * public boolean onSave() {
+     *     // Specify redirect parameters
+     *     Map parameters = new HashMap();
+     *     parameters.put("customerId", getCustomerId());
+     *
+     *     // Set redirect to customer.htm page
+     *     setRedirect("/customer.htm", parameters);
+     *
+     *     return false;
+     * } </pre>
+     *
+     * To render multiple parameter values for the same parameter name, specify
+     * the values as a String[] array. For example:
+     *
+     * <pre class="prettyprint">
+     * public boolean onSave() {
+     *
+     *     // Specify an array of customer IDs
+     *     String[] ids = {"123", "456", "789"};
+     *
+     *     // Specify redirect parameters
+     *     Map parameters = new HashMap();
+     *     parameters.put("customerIds", ids);
+     *
+     *     // Set redirect to customer.htm page
+     *     setRedirect("/customer.htm", parameters);
+     *
+     *     return false;
+     * } </pre>
      *
      * @see #setRedirect(java.lang.String)
      *
@@ -1126,9 +1159,9 @@
      * @param params the map of request parameter name and value pairs
      */
     public void setRedirect(String location, Map params) {
+        Context context = getContext();
         if (StringUtils.isNotBlank(location)) {
             if (location.charAt(0) == '/') {
-                Context context = getContext();
                 String contextPath = context.getRequest().getContextPath();
 
                 // Guard against adding duplicate context path
@@ -1144,14 +1177,28 @@
             for (Iterator i = params.keySet().iterator(); i.hasNext();) {
                 String paramName = i.next().toString();
                 Object paramValue = params.get(paramName);
-                if (paramValue != null) {
-                    buffer.append(paramName);
-                    buffer.append("=");
-                    buffer.append(paramValue);
-                    if (i.hasNext()) {
-                        buffer.append("&");
+
+                // Check for multivalued parameter
+                if (paramValue instanceof String[]) {
+                    String[] paramValues = (String[]) paramValue;
+                    for (int j = 0; j < paramValues.length; j++) {
+                        buffer.append(paramName);
+                        buffer.append("=");
+                        buffer.append(ClickUtils.encodeUrl(paramValues[j], context));
+                        if (j < paramValues.length - 1) {
+                            buffer.append("&");
+                        }
+                    }
+                } else {
+                    if (paramValue != null) {
+                        buffer.append(paramName);
+                        buffer.append("=");
+                        buffer.append(ClickUtils.encodeUrl(paramValue, context));
                     }
                 }
+                if (i.hasNext()) {
+                    buffer.append("&");
+                }
             }
 
             if (buffer.length() > 0) {
@@ -1173,7 +1220,9 @@
      * The map keys will be used as the request parameter names and the map
      * values will be used as the request parameter values.
      *
+     * @see #setRedirect(java.lang.String, java.util.Map)
      * @see #setRedirect(java.lang.String)
+     *
      * @param pageClass the class of the Page to redirect the request to
      * @param params the map of request parameter name and value pairs
      * @throws IllegalArgumentException if the Page Class is not configured

Modified: incubator/click/trunk/click/framework/src/org/apache/click/control/AbstractLink.java
URL: http://svn.apache.org/viewvc/incubator/click/trunk/click/framework/src/org/apache/click/control/AbstractLink.java?rev=779697&r1=779696&r2=779697&view=diff
==============================================================================
--- incubator/click/trunk/click/framework/src/org/apache/click/control/AbstractLink.java (original)
+++ incubator/click/trunk/click/framework/src/org/apache/click/control/AbstractLink.java Thu May 28 17:49:50 2009
@@ -18,9 +18,13 @@
  */
 package org.apache.click.control;
 
+import java.util.Enumeration;
 import java.util.HashMap;
+import java.util.Iterator;
 import java.util.Map;
 
+import javax.servlet.http.HttpServletRequest;
+import org.apache.click.Context;
 import org.apache.click.util.ClickUtils;
 import org.apache.click.util.HtmlStringBuffer;
 
@@ -274,7 +278,22 @@
      */
     public String getParameter(String name) {
         if (hasParameters()) {
-            return (String) getParameters().get(name);
+            Object value = getParameters().get(name);
+
+            if (value instanceof String) {
+                return (String) value;
+            }
+
+            if (value instanceof String[]) {
+                String[] array = (String[]) value;
+                if (array.length >= 1) {
+                    return array[0];
+                } else {
+                    return null;
+                }
+            }
+
+            return (value == null ? null : value.toString());
         } else {
             return null;
         }
@@ -317,6 +336,51 @@
     }
 
     /**
+     * Return the link request parameter values for the given name, or null if
+     * the parameter values does not exist.
+     *
+     * @param name the name of request parameter
+     * @return the link request parameter values
+     */
+    public String[] getParameterValues(String name) {
+        if (hasParameters()) {
+            Object values = getParameters().get(name);
+            if (values instanceof String) {
+                return new String[] { values.toString() };
+            }
+            if (values instanceof String[]) {
+                return (String[]) values;
+            } else {
+                return null;
+            }
+        } else {
+            return null;
+        }
+    }
+
+    /**
+     * Set the link parameter with the given parameter name and values. If the
+     * values are null, the parameter will be removed from the {@link #parameters}.
+     *
+     * @see #setParameter(java.lang.String, java.lang.String)
+     *
+     * @param name the attribute name
+     * @param values the attribute values
+     * @throws IllegalArgumentException if name parameter is null
+     */
+    public void setParameterValues(String name, String[] values) {
+        if (name == null) {
+            throw new IllegalArgumentException("Null name parameter");
+        }
+
+        if (values != null) {
+            getParameters().put(name, values);
+        } else {
+            getParameters().remove(name);
+        }
+    }
+
+    /**
      * Return the AbstractLink parameters Map.
      *
      * @return the AbstractLink parameters Map
@@ -510,4 +574,76 @@
             buffer.elementEnd(getTag());
         }
     }
+
+    // ------------------------------------------------------ Protected Methods
+
+    /**
+     * Render the given link parameters to the buffer.
+     * <p/>
+     * The parameters will be rendered as URL key/value pairs e.g:
+     * "<tt>firstname=john&lastname=smith</tt>".
+     * <p/>
+     * Multivalued parameters will be rendered with each value sharing the same
+     * key e.g: "<tt>name=john&name=susan&name=mary</tt>".
+     * <p/>
+     * The parameter value will be encoded through
+     * {@link org.apache.click.util.ClickUtils#encodeUrl(java.lang.Object, org.apache.click.Context)}.
+     *
+     * @param buffer the buffer to render the parameters to
+     * @param parameters the parameters to render
+     * @param context the request context
+     */
+    protected void renderParameters(HtmlStringBuffer buffer, Map parameters,
+        Context context) {
+
+        Iterator i = parameters.keySet().iterator();
+        while (i.hasNext()) {
+            String paramName = i.next().toString();
+            Object paramValue = getParameters().get(paramName);
+
+            // Check for multivalued parameter
+            if (paramValue instanceof String[]) {
+                String[] paramValues = (String[]) paramValue;
+                for (int j = 0; j < paramValues.length; j++) {
+                    buffer.append(paramName);
+                    buffer.append("=");
+                    buffer.append(ClickUtils.encodeUrl(paramValues[j], context));
+                    if (j < paramValues.length - 1) {
+                        buffer.append("&amp;");
+                    }
+                }
+            } else {
+                if (paramValue != null) {
+                    buffer.append(paramName);
+                    buffer.append("=");
+                    buffer.append(ClickUtils.encodeUrl(paramValue, context));
+                }
+            }
+            if (i.hasNext()) {
+                buffer.append("&amp;");
+            }
+        }
+    }
+
+    /**
+     * This method binds the submitted request parameters to the link's
+     * parameters.
+     *
+     * @param context the request context
+     */
+    protected void bindRequestParameters(Context context) {
+        HttpServletRequest request = context.getRequest();
+        Enumeration paramNames = request.getParameterNames();
+
+        while (paramNames.hasMoreElements()) {
+            String param = paramNames.nextElement().toString();
+            String[] values = request.getParameterValues(param);
+
+            if (values != null && values.length == 1) {
+                getParameters().put(param, values[0]);
+            } else {
+                getParameters().put(param, values);
+            }
+        }
+    }
 }

Modified: incubator/click/trunk/click/framework/src/org/apache/click/control/ActionLink.java
URL: http://svn.apache.org/viewvc/incubator/click/trunk/click/framework/src/org/apache/click/control/ActionLink.java?rev=779697&r1=779696&r2=779697&view=diff
==============================================================================
--- incubator/click/trunk/click/framework/src/org/apache/click/control/ActionLink.java (original)
+++ incubator/click/trunk/click/framework/src/org/apache/click/control/ActionLink.java Thu May 28 17:49:50 2009
@@ -18,11 +18,8 @@
  */
 package org.apache.click.control;
 
-import java.util.Enumeration;
 import java.util.Iterator;
 
-import javax.servlet.http.HttpServletRequest;
-
 import org.apache.click.Context;
 import org.apache.click.util.ClickUtils;
 import org.apache.click.util.HtmlStringBuffer;
@@ -314,15 +311,29 @@
         if (hasParameters()) {
             Iterator i = getParameters().keySet().iterator();
             while (i.hasNext()) {
-                String name = i.next().toString();
-                if (!name.equals(ACTION_LINK) && !name.equals(VALUE)) {
-                    Object paramValue = getParameters().get(name);
-                    String encodedValue =
-                        ClickUtils.encodeUrl(paramValue, context);
-                    buffer.append("&amp;");
-                    buffer.append(name);
-                    buffer.append("=");
-                    buffer.append(encodedValue);
+                String paramName = i.next().toString();
+                if (!paramName.equals(ACTION_LINK) && !paramName.equals(VALUE)) {
+                    Object paramValue = getParameters().get(paramName);
+
+                    // Check for multivalued parameter
+                    if (paramValue instanceof String[]) {
+                        String[] paramValues = (String[]) paramValue;
+                        for (int j = 0; j < paramValues.length; j++) {
+                            buffer.append("&amp;");
+                            buffer.append(paramName);
+                            buffer.append("=");
+                            buffer.append(ClickUtils.encodeUrl(paramValues[j],
+                                context));
+                        }
+                    } else {
+                        if (paramValue != null) {
+                            buffer.append("&amp;");
+                            buffer.append(paramName);
+                            buffer.append("=");
+                            buffer.append(ClickUtils.encodeUrl(paramValue,
+                                                               context));
+                        }
+                    }
                 }
             }
         }
@@ -388,11 +399,7 @@
      * @return the ActionLink value if the ActionLink was processed
      */
     public String getValue() {
-        if (hasParameters()) {
-            return (String) getParameters().get(VALUE);
-        } else {
-            return null;
-        }
+        return getParameter(VALUE);
     }
 
     /**
@@ -404,8 +411,9 @@
      * @throws NumberFormatException if the value cannot be parsed into a Double
      */
     public Double getValueDouble() {
-        if (getValue() != null) {
-            return Double.valueOf(getValue());
+        String value = getValue();
+        if (value != null) {
+            return Double.valueOf(value);
         } else {
             return null;
         }
@@ -420,8 +428,9 @@
      * @throws NumberFormatException if the value cannot be parsed into an Integer
      */
     public Integer getValueInteger() {
-        if (getValue() != null) {
-            return Integer.valueOf(getValue());
+        String value = getValue();
+        if (value != null) {
+            return Integer.valueOf(value);
         } else {
             return null;
         }
@@ -436,8 +445,9 @@
      * @throws NumberFormatException if the value cannot be parsed into a Long
      */
     public Long getValueLong() {
-        if (getValue() != null) {
-            return Long.valueOf(getValue());
+        String value = getValue();
+        if (value != null) {
+            return Long.valueOf(value);
         } else {
             return null;
         }
@@ -449,11 +459,7 @@
      * @param value the ActionLink value
      */
     public void setValue(String value) {
-        if (value != null) {
-            getParameters().put(VALUE, value);
-        } else {
-            getParameters().remove(VALUE);
-        }
+        setParameter(VALUE, value);
     }
 
     /**
@@ -482,14 +488,7 @@
         clicked = getName().equals(context.getRequestParameter(ACTION_LINK));
 
         if (clicked) {
-            HttpServletRequest request = context.getRequest();
-            Enumeration paramNames = request.getParameterNames();
-
-            while (paramNames.hasMoreElements()) {
-                String name = paramNames.nextElement().toString();
-                String value = request.getParameter(name);
-                getParameters().put(name, value);
-            }
+            bindRequestParameters(context);
         }
     }
 

Modified: incubator/click/trunk/click/framework/src/org/apache/click/control/PageLink.java
URL: http://svn.apache.org/viewvc/incubator/click/trunk/click/framework/src/org/apache/click/control/PageLink.java?rev=779697&r1=779696&r2=779697&view=diff
==============================================================================
--- incubator/click/trunk/click/framework/src/org/apache/click/control/PageLink.java (original)
+++ incubator/click/trunk/click/framework/src/org/apache/click/control/PageLink.java Thu May 28 17:49:50 2009
@@ -18,12 +18,8 @@
  */
 package org.apache.click.control;
 
-import java.util.Iterator;
-import java.util.Map;
-
 import org.apache.click.ActionListener;
 import org.apache.click.Context;
-import org.apache.click.util.ClickUtils;
 import org.apache.click.util.HtmlStringBuffer;
 
 import org.apache.commons.lang.StringUtils;
@@ -154,19 +150,7 @@
         if (hasParameters()) {
             buffer.append("?");
 
-            Iterator i = getParameters().entrySet().iterator();
-            while (i.hasNext()) {
-                Map.Entry entry = (Map.Entry) i.next();
-                String name = entry.getKey().toString();
-                String value = entry.getValue().toString();
-
-                buffer.append(name);
-                buffer.append("=");
-                buffer.append(ClickUtils.encodeUrl(value, context));
-                if (i.hasNext()) {
-                    buffer.append("&amp;");
-                }
-            }
+            renderParameters(buffer, getParameters(), context);
         }
 
         return context.getResponse().encodeURL(buffer.toString());