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 2010/06/08 13:06:49 UTC

svn commit: r952604 - in /click/trunk/click/framework/src/org/apache/click/control: AbstractLink.java ActionLink.java

Author: sabob
Date: Tue Jun  8 11:06:49 2010
New Revision: 952604

URL: http://svn.apache.org/viewvc?rev=952604&view=rev
Log:
added strict parameter binding option. CLK-685

Modified:
    click/trunk/click/framework/src/org/apache/click/control/AbstractLink.java
    click/trunk/click/framework/src/org/apache/click/control/ActionLink.java

Modified: click/trunk/click/framework/src/org/apache/click/control/AbstractLink.java
URL: http://svn.apache.org/viewvc/click/trunk/click/framework/src/org/apache/click/control/AbstractLink.java?rev=952604&r1=952603&r2=952604&view=diff
==============================================================================
--- click/trunk/click/framework/src/org/apache/click/control/AbstractLink.java (original)
+++ click/trunk/click/framework/src/org/apache/click/control/AbstractLink.java Tue Jun  8 11:06:49 2010
@@ -18,10 +18,10 @@
  */
 package org.apache.click.control;
 
-import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.Map;
+import java.util.Set;
 
 import javax.servlet.http.HttpServletRequest;
 import org.apache.click.Context;
@@ -74,6 +74,14 @@ public abstract class AbstractLink exten
     /** Flag to set if both icon and text are rendered, default value is false. */
     protected boolean renderLabelAndImage = false;
 
+    /**
+     * Flag indicating whether incoming request parameters are only bound to the
+     * link {@link #parameters} if they have been defined before the
+     * {@link #onProcess()} event. Strict parameter binding will be applied for
+     * ajax requests, while non-strict binding is used for non-ajax requests.
+     */
+    protected Boolean strictParameterBinding = null;
+
     // Constructors -----------------------------------------------------------
 
     /**
@@ -267,6 +275,77 @@ public abstract class AbstractLink exten
     }
 
     /**
+     * Return true if strict parameter binding is used, false otherwise.
+     *
+     * @see {@link #setStrictParameterBinding(boolean)} for more information
+     *
+     * @return true if strict parameter binding is used, false otherwise
+     */
+    public boolean isStrictParameterBinding() {
+        if (strictParameterBinding == null) {
+            return Boolean.FALSE;
+        }
+        return strictParameterBinding;
+    }
+
+    /**
+     * Set whether strict parameter binding should be used. By default strict
+     * parameter binding is used for ajax requests, while non-strict binding is
+     * used for non-ajax requests.
+     * <p/>
+     * Strict parameter binding means that incoming request parameters are only
+     * added to the link {@link #parameters parameter map} if these parameters
+     * have been defined <i>before</i> the {@link #onProcess()} event.
+     * <p/>
+     * A link parameter is automatically defined when
+     * {@link #setParameter(java.lang.String, java.lang.String) setting a parameter}.
+     * Alternatively a parameter can be explicitly defined via
+     * {@link #defineParameter(java.lang.String)}.
+     * <p/>
+     * For example:
+     * <pre class="prettyprint">
+     * private ActionLink link = new ActionLink("link");
+     *
+     * public void onInit() {
+     *     link.defineParameter("id"); // Explicitly defined parameter
+     *     link.setParameter("customerName", "John"); // Implicitly defined parameter
+     * } </pre>
+     *
+     * @param value true if strict parameter binding should be used, false
+     * otherwise
+     */
+    public void setStrictParameterBinding(boolean value) {
+        this.strictParameterBinding = value;
+    }
+
+    /**
+     * Defines a link parameter that will be bound to its matching request
+     * parameter.
+     * <p/>
+     * <b>Please note:</b> by default parameters only need to be defined for
+     * ajax requests. For non-ajax requests, <i>all</i> incoming request parameters
+     * are bound. This behavior can be controlled through the
+     * {@link #setStrictParameterBinding(boolean) strictParameterBinding}
+     * property.
+     * <p/>
+     * <b>Also note:</b> parameters must be defined <i>before</i> the
+     * {@link #onProcess()} event, otherwise they will not be bound to
+     * incoming request parameters.
+     *
+     * @param name the name of the parameter to define
+     */
+    public void defineParameter(String name) {
+        if (name == null) {
+            throw new IllegalArgumentException("Null name parameter");
+        }
+
+        Map<String, Object> parameters = getParameters();
+        if (!parameters.containsKey(name)) {
+            parameters.put(name, null);
+        }
+    }
+
+    /**
      * Return the link request parameter value for the given name, or null if
      * the parameter value does not exist.
      *
@@ -660,10 +739,24 @@ public abstract class AbstractLink exten
     @SuppressWarnings("unchecked")
     protected void bindRequestParameters(Context context) {
         HttpServletRequest request = context.getRequest();
-        Enumeration paramNames = request.getParameterNames();
 
-        while (paramNames.hasMoreElements()) {
-            String param = paramNames.nextElement().toString();
+        Set<String> parameterNames = null;
+
+        if (strictParameterBinding == null) {
+            if (getContext().isAjaxRequest()) {
+                parameterNames = getParameters().keySet();
+            } else {
+                parameterNames = request.getParameterMap().keySet();
+            }
+        } else {
+            if (isStrictParameterBinding()) {
+                parameterNames = getParameters().keySet();
+            } else {
+                parameterNames = request.getParameterMap().keySet();
+            }
+        }
+
+        for (String param : parameterNames) {
             String[] values = request.getParameterValues(param);
 
             if (values != null && values.length == 1) {

Modified: click/trunk/click/framework/src/org/apache/click/control/ActionLink.java
URL: http://svn.apache.org/viewvc/click/trunk/click/framework/src/org/apache/click/control/ActionLink.java?rev=952604&r1=952603&r2=952604&view=diff
==============================================================================
--- click/trunk/click/framework/src/org/apache/click/control/ActionLink.java (original)
+++ click/trunk/click/framework/src/org/apache/click/control/ActionLink.java Tue Jun  8 11:06:49 2010
@@ -510,4 +510,17 @@ public class ActionLink extends Abstract
         return true;
     }
 
+    // Protected Methods ------------------------------------------------------
+
+    /**
+     * This method binds the submitted request parameters to the link's
+     * parameters.
+     *
+     * @param context the request context
+     */
+    @Override
+    protected void bindRequestParameters(Context context) {
+        defineParameter(VALUE);
+        super.bindRequestParameters(context);
+    }
 }