Author: hlship
Date: Thu Aug 11 19:06:57 2011
New Revision: 1156744

URL: http://svn.apache.org/viewvc?rev=1156744&view=rev
Log:
TAP5-832: Support a nulls parameter on Hidden to correctly handle null values 
sent to or received from the client inside the field

Modified:
    
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Hidden.java

Modified: 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Hidden.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Hidden.java?rev=1156744&r1=1156743&r2=1156744&view=diff
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Hidden.java
 (original)
+++ 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Hidden.java
 Thu Aug 11 19:06:57 2011
@@ -15,11 +15,12 @@
 package org.apache.tapestry5.corelib.components;
 
 import org.apache.tapestry5.*;
-import org.apache.tapestry5.dom.Element;
 import org.apache.tapestry5.annotations.Environmental;
 import org.apache.tapestry5.annotations.Parameter;
 import org.apache.tapestry5.annotations.SupportsInformalParameters;
+import org.apache.tapestry5.dom.Element;
 import org.apache.tapestry5.ioc.annotations.Inject;
+import org.apache.tapestry5.ioc.internal.util.InternalUtils;
 import org.apache.tapestry5.services.ComponentDefaultProvider;
 import org.apache.tapestry5.services.FormSupport;
 import org.apache.tapestry5.services.Request;
@@ -30,9 +31,9 @@ import org.apache.tapestry5.services.jav
  * {@linkplain org.apache.tapestry5.ValueEncoder#toClient(Object) encoded} 
when rendered, then decoded when the form is
  * submitted,
  * and the value parameter updated.
- * 
- * @since 5.1.0.2
+ *
  * @tapestrydoc
+ * @since 5.1.0.2
  */
 @SupportsInformalParameters
 public class Hidden implements ClientElement
@@ -44,6 +45,14 @@ public class Hidden implements ClientEle
     private Object value;
 
     /**
+     * Defines how nulls on the server side, or sent from the client side, are 
treated. The selected strategy may
+     * replace the nulls with some other value. The default strategy leaves 
nulls alone.  Another built-in strategy,
+     * zero, replaces nulls with the value 0.
+     */
+    @Parameter(defaultPrefix = BindingConstants.NULLFIELDSTRATEGY, value = 
"default")
+    private NullFieldStrategy nulls;
+
+    /**
      * Value encoder for the value, usually determined automatically from the 
type of the property bound to the value
      * parameter.
      */
@@ -94,7 +103,9 @@ public class Hidden implements ClientEle
     boolean beginRender(MarkupWriter writer)
     {
         if (formSupport == null)
+        {
             throw new RuntimeException("The Hidden component must be enclosed 
by a Form component.");
+        }
 
         controlName = formSupport.allocateControlName(resources.getId());
 
@@ -102,7 +113,9 @@ public class Hidden implements ClientEle
 
         formSupport.store(this, new ProcessSubmission(controlName));
 
-        String encoded = encoder.toClient(value);
+        Object toEncode = value == null ? nulls.replaceToClient() : value;
+
+        String encoded = toEncode == null ? "" : encoder.toClient(toEncode);
 
         hiddenInputElement = writer.element("input", "type", "hidden", "name", 
controlName, "value", encoded);
 
@@ -117,7 +130,9 @@ public class Hidden implements ClientEle
     {
         String encoded = request.getParameter(controlName);
 
-        Object decoded = encoder.toValue(encoded);
+        String toDecode = InternalUtils.isBlank(encoded) ? 
nulls.replaceFromClient() : encoded;
+
+        Object decoded = toDecode == null ? null : encoder.toValue(toDecode);
 
         value = decoded;
     }


Reply via email to