struts-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "ASF GitHub Bot (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (WW-4865) Struts s:checkbox conversion fails to List<Integer>
Date Fri, 23 Mar 2018 19:52:00 GMT

    [ https://issues.apache.org/jira/browse/WW-4865?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16411955#comment-16411955
] 

ASF GitHub Bot commented on WW-4865:
------------------------------------

aleksandr-m closed pull request #169: WW-4865: Add "requiredValue" parameter to Checkbox Struts
Tag
URL: https://github.com/apache/struts/pull/169
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/core/src/main/java/org/apache/struts2/components/Checkbox.java b/core/src/main/java/org/apache/struts2/components/Checkbox.java
index a9f0fdc6b..b93fab309 100644
--- a/core/src/main/java/org/apache/struts2/components/Checkbox.java
+++ b/core/src/main/java/org/apache/struts2/components/Checkbox.java
@@ -60,6 +60,7 @@
     final public static String TEMPLATE = "checkbox";
 
     protected String fieldValue;
+    protected String submitUnchecked;
 
     public Checkbox(ValueStack stack, HttpServletRequest request, HttpServletResponse response)
{
         super(stack, request, response);
@@ -75,6 +76,11 @@ protected void evaluateExtraParams() {
         } else {
             addParameter("fieldValue", "true");
         }
+        if (submitUnchecked != null) {
+            addParameter("submitUnchecked", findString(submitUnchecked));
+        } else {
+            addParameter("submitUnchecked", "false");
+        }
     }
 
     protected Class getValueClassType() {
@@ -86,4 +92,8 @@ public void setFieldValue(String fieldValue) {
         this.fieldValue = fieldValue;
     }
 
+    @StrutsTagAttribute(description="If set to true, unchecked elements will be submitted
with the form.", defaultValue="false")
+    public void setSubmitUnchecked(String submitUnchecked) {
+        this.submitUnchecked = submitUnchecked;
+    }
 }
diff --git a/core/src/main/java/org/apache/struts2/views/jsp/ui/CheckboxTag.java b/core/src/main/java/org/apache/struts2/views/jsp/ui/CheckboxTag.java
index ccf31bb2b..55d8104ac 100644
--- a/core/src/main/java/org/apache/struts2/views/jsp/ui/CheckboxTag.java
+++ b/core/src/main/java/org/apache/struts2/views/jsp/ui/CheckboxTag.java
@@ -38,6 +38,7 @@
     private static final long serialVersionUID = -350752809266337636L;
 
     protected String fieldValue;
+    protected String submitUnchecked;
 
     public Component getBean(ValueStack stack, HttpServletRequest req, HttpServletResponse
res) {
         return new Checkbox(stack, req, res);
@@ -47,9 +48,14 @@ protected void populateParams() {
         super.populateParams();
 
         ((Checkbox) component).setFieldValue(fieldValue);
+        ((Checkbox) component).setSubmitUnchecked(submitUnchecked);
     }
 
     public void setFieldValue(String aValue) {
         this.fieldValue = aValue;
     }
+
+    public void setSubmitUnchecked(String aValue) {
+        this.submitUnchecked = aValue;
+    }
 }
diff --git a/core/src/main/resources/template/simple/checkbox.ftl b/core/src/main/resources/template/simple/checkbox.ftl
index fd4781044..820f2316a 100644
--- a/core/src/main/resources/template/simple/checkbox.ftl
+++ b/core/src/main/resources/template/simple/checkbox.ftl
@@ -40,8 +40,11 @@
 <#include "/${parameters.templateDir}/${parameters.expandTheme}/scripting-events.ftl"
/>
 <#include "/${parameters.templateDir}/${parameters.expandTheme}/common-attributes.ftl"
/>
 <#include "/${parameters.templateDir}/${parameters.expandTheme}/dynamic-attributes.ftl"
/>
-/><input type="hidden" id="__checkbox_${parameters.id?html}" name="__checkbox_${parameters.name?html}"
value="${parameters.fieldValue?html}"<#rt/>
+/>
+<#if (parameters.submitUnchecked?boolean)!false>
+<input type="hidden" id="__checkbox_${parameters.id?html}" name="__checkbox_${parameters.name?html}"
value="${parameters.fieldValue?html}"<#rt/>
 <#if parameters.disabled!false>
  disabled="disabled"<#rt/>
 </#if>
- />
\ No newline at end of file
+ />
+</#if>
\ No newline at end of file
diff --git a/core/src/site/resources/tags/checkbox.html b/core/src/site/resources/tags/checkbox.html
index 75cd10679..204d35cac 100644
--- a/core/src/site/resources/tags/checkbox.html
+++ b/core/src/site/resources/tags/checkbox.html
@@ -297,6 +297,14 @@ <h2>Attributes</h2>
 					<td align="left" valign="top">String</td>
 					<td align="left" valign="top">The css style definitions for element to use - it's
an alias of cssStyle attribute.</td>
 				</tr>
+				<tr>
+					<td align="left" valign="top">submitUnchecked</td>
+					<td align="left" valign="top">false</td>
+					<td align="left" valign="top">false</td>
+					<td align="left" valign="top">false</td>
+					<td align="left" valign="top">String</td>
+					<td align="left" valign="top">If set to true, unchecked elements will be submitted
with the form.</td>
+				</tr>
 				<tr>
 					<td align="left" valign="top">tabindex</td>
 					<td align="left" valign="top">false</td>
diff --git a/core/src/test/java/org/apache/struts2/views/jsp/ui/CheckboxTest.java b/core/src/test/java/org/apache/struts2/views/jsp/ui/CheckboxTest.java
index d6971e8e0..1d6402027 100644
--- a/core/src/test/java/org/apache/struts2/views/jsp/ui/CheckboxTest.java
+++ b/core/src/test/java/org/apache/struts2/views/jsp/ui/CheckboxTest.java
@@ -197,4 +197,48 @@ public void testDisabled() throws Exception {
 
         verify(CheckboxTag.class.getResource("Checkbox-6.txt"));
     }
+
+    public void testSubmitUncheckedAsFalse() throws Exception {
+        TestAction testAction = (TestAction) action;
+        testAction.setFoo("true");
+
+        CheckboxTag tag = new CheckboxTag();
+        tag.setPageContext(pageContext);
+        tag.setLabel("mylabel");
+        tag.setName("foo");
+        tag.setFieldValue("baz");
+        //tag.setSubmitUnchecked("false");        // Test default value
+        tag.setTitle("mytitle");
+        tag.setDisabled("true");
+
+        tag.doStartTag();
+        tag.doEndTag();
+
+        verify(CheckboxTag.class.getResource("Checkbox-7.txt"));
+
+        // Test value set
+        tag.setSubmitUnchecked("false");
+        verify(CheckboxTag.class.getResource("Checkbox-7.txt"));
+
+    }
+
+    public void testSubmitUncheckedAsTrue() throws Exception {
+        TestAction testAction = (TestAction) action;
+        testAction.setFoo("true");
+
+        CheckboxTag tag = new CheckboxTag();
+        tag.setPageContext(pageContext);
+        tag.setLabel("mylabel");
+        tag.setName("foo");
+        tag.setFieldValue("baz");
+        tag.setSubmitUnchecked("true");
+        tag.setTitle("mytitle");
+        tag.setDisabled("true");
+
+        tag.doStartTag();
+        tag.doEndTag();
+
+        verify(CheckboxTag.class.getResource("Checkbox-8.txt"));
+    }
+
 }
diff --git a/core/src/test/resources/org/apache/struts2/views/jsp/ui/Checkbox-1.txt b/core/src/test/resources/org/apache/struts2/views/jsp/ui/Checkbox-1.txt
index b746088d2..b0dd27a4f 100644
--- a/core/src/test/resources/org/apache/struts2/views/jsp/ui/Checkbox-1.txt
+++ b/core/src/test/resources/org/apache/struts2/views/jsp/ui/Checkbox-1.txt
@@ -3,7 +3,6 @@
 	</td>
     <td class="tdCheckboxInput">
 		<input type="checkbox" name="foo" value="baz" checked="checked" id="someId" title="mytitle"
onfocus="test();"/>
-		<input type="hidden" id="__checkbox_someId" name="__checkbox_foo" value="baz"/>
         <label for="someId" class="checkboxLabel">mylabel</label>
     </td>
 </tr>
diff --git a/core/src/test/resources/org/apache/struts2/views/jsp/ui/Checkbox-2.txt b/core/src/test/resources/org/apache/struts2/views/jsp/ui/Checkbox-2.txt
index caf46327b..12dc47232 100644
--- a/core/src/test/resources/org/apache/struts2/views/jsp/ui/Checkbox-2.txt
+++ b/core/src/test/resources/org/apache/struts2/views/jsp/ui/Checkbox-2.txt
@@ -3,7 +3,6 @@
 	</td>
     <td class="tdCheckboxInput">
         <input type="checkbox" name="foo" value="baz" id="foo" title="mytitle"/>
-        <input type="hidden" id="__checkbox_foo" name="__checkbox_foo" value="baz"/>
         <label for="foo" class="checkboxLabel">mylabel</label>
     </td>
 </tr>
diff --git a/core/src/test/resources/org/apache/struts2/views/jsp/ui/Checkbox-3.txt b/core/src/test/resources/org/apache/struts2/views/jsp/ui/Checkbox-3.txt
index 68b91f323..173384b12 100644
--- a/core/src/test/resources/org/apache/struts2/views/jsp/ui/Checkbox-3.txt
+++ b/core/src/test/resources/org/apache/struts2/views/jsp/ui/Checkbox-3.txt
@@ -10,7 +10,6 @@
 	</td>
     <td class="tdCheckboxInput">
           <input type="checkbox" name="foo" value="baz" checked="checked" id="foo" class="myErrorClass"
title="mytitle" onclick="test();" ondblclick="test();"/>
-          <input type="hidden" id="__checkbox_foo" name="__checkbox_foo" value="baz"/>
           <label for="foo" class="checkboxErrorLabel">mylabel</label>
     </td>
 </tr>
diff --git a/core/src/test/resources/org/apache/struts2/views/jsp/ui/Checkbox-33.txt b/core/src/test/resources/org/apache/struts2/views/jsp/ui/Checkbox-33.txt
index ef05dc427..7dc28c391 100644
--- a/core/src/test/resources/org/apache/struts2/views/jsp/ui/Checkbox-33.txt
+++ b/core/src/test/resources/org/apache/struts2/views/jsp/ui/Checkbox-33.txt
@@ -10,7 +10,6 @@
 	</td>
     <td class="tdCheckboxInput">
           <input type="checkbox" name="foo" value="baz" checked="checked" id="foo" style="color:red"
title="mytitle" onclick="test();" ondblclick="test();"/>
-          <input type="hidden" id="__checkbox_foo" name="__checkbox_foo" value="baz"/>
           <label for="foo" class="checkboxErrorLabel">mylabel</label>
     </td>
 </tr>
diff --git a/core/src/test/resources/org/apache/struts2/views/jsp/ui/Checkbox-4.txt b/core/src/test/resources/org/apache/struts2/views/jsp/ui/Checkbox-4.txt
index 6cf1f955a..e9dc9de40 100644
--- a/core/src/test/resources/org/apache/struts2/views/jsp/ui/Checkbox-4.txt
+++ b/core/src/test/resources/org/apache/struts2/views/jsp/ui/Checkbox-4.txt
@@ -6,6 +6,5 @@
 <tr>
     <td colspan="2">
 		<input type="checkbox" name="foo" value="baz" checked="checked" id="someId" title="mytitle"
onfocus="test();"/>
-		<input type="hidden" id="__checkbox_someId" name="__checkbox_foo" value="baz"/>
     </td>
 </tr>
diff --git a/core/src/test/resources/org/apache/struts2/views/jsp/ui/Checkbox-5.txt b/core/src/test/resources/org/apache/struts2/views/jsp/ui/Checkbox-5.txt
index 49e093cae..5bfdc74a1 100644
--- a/core/src/test/resources/org/apache/struts2/views/jsp/ui/Checkbox-5.txt
+++ b/core/src/test/resources/org/apache/struts2/views/jsp/ui/Checkbox-5.txt
@@ -4,6 +4,5 @@
 	</td>
     <td class="tdCheckboxInput">
 		<input type="checkbox" name="foo" value="baz" checked="checked" id="someId" title="mytitle"
onfocus="test();"/>
-		<input type="hidden" id="__checkbox_someId" name="__checkbox_foo" value="baz"/>
     </td>
 </tr>
diff --git a/core/src/test/resources/org/apache/struts2/views/jsp/ui/Checkbox-6.txt b/core/src/test/resources/org/apache/struts2/views/jsp/ui/Checkbox-6.txt
index c54c8629a..0c6d47068 100644
--- a/core/src/test/resources/org/apache/struts2/views/jsp/ui/Checkbox-6.txt
+++ b/core/src/test/resources/org/apache/struts2/views/jsp/ui/Checkbox-6.txt
@@ -3,7 +3,6 @@
 	</td>
     <td class="tdCheckboxInput">
 		<input type="checkbox" name="foo" value="baz" checked="checked" disabled="disabled"
id="foo" title="mytitle" />
-		<input type="hidden" id="__checkbox_foo" name="__checkbox_foo" value="baz" disabled="disabled"/>
 		<label for="foo" class="checkboxLabel">mylabel</label>
     </td>
 </tr>
diff --git a/core/src/test/resources/org/apache/struts2/views/jsp/ui/Checkbox-7.txt b/core/src/test/resources/org/apache/struts2/views/jsp/ui/Checkbox-7.txt
new file mode 100644
index 000000000..e19103b1e
--- /dev/null
+++ b/core/src/test/resources/org/apache/struts2/views/jsp/ui/Checkbox-7.txt
@@ -0,0 +1,8 @@
+<tr>
+	<td class="tdCheckboxLabel">
+	</td>
+    <td class="tdCheckboxInput">
+		<input type="checkbox" name="foo" value="baz" checked="checked" disabled="disabled"
id="foo" title="mytitle"/>
+		<label for="foo" class="checkboxLabel">mylabel</label>
+    </td>
+</tr>
diff --git a/core/src/test/resources/org/apache/struts2/views/jsp/ui/Checkbox-8.txt b/core/src/test/resources/org/apache/struts2/views/jsp/ui/Checkbox-8.txt
new file mode 100644
index 000000000..4dad94d17
--- /dev/null
+++ b/core/src/test/resources/org/apache/struts2/views/jsp/ui/Checkbox-8.txt
@@ -0,0 +1,9 @@
+<tr>
+	<td class="tdCheckboxLabel">
+	</td>
+	<td class="tdCheckboxInput">
+		<input type="checkbox" name="foo" value="baz" checked="checked" disabled="disabled"
id="foo" title="mytitle"/>
+		<input type="hidden" id="__checkbox_foo" name="__checkbox_foo" value="baz" disabled="disabled"/>
+		<label for="foo" class="checkboxLabel">mylabel</label>
+	</td>
+</tr>
\ No newline at end of file


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


> Struts s:checkbox conversion fails to List<Integer>
> ---------------------------------------------------
>
>                 Key: WW-4865
>                 URL: https://issues.apache.org/jira/browse/WW-4865
>             Project: Struts 2
>          Issue Type: Bug
>          Components: Core, Core Tags
>    Affects Versions: 2.5.13
>            Reporter: Burton Rhodes
>            Priority: Minor
>             Fix For: 2.6
>
>   Original Estimate: 24h
>  Remaining Estimate: 24h
>
> Struts <s:checkbox> is not converted correctly to List<Integer> when only
one checkbox element is present and checkbox is not checked.
> [Given]
> 1) Mapping s:checkbox to a List<Integer>
> 2) there is only one checkbox in the list
> 3) the checkbox is not checked when the form is submitted.  
> [Result]
> Input invalid
> The issue is caused by the checkbox tag using the "submit unchecked values" design pattern.
 As a result, the code base tries to convert the value of the single unchecked element to
false (rather than not setting any value of the List<Integer>).
> Fix: Allow the developer to choose if the "submit unchecked values" is to be implemented.
 Add a new parameter to the s:checkbox tag ("requiredValue").  Default of requiredValue=true
for backwards compatibility.  When set to false, no hidden input values will be displayed
in the view.  This flexibility might be needed in other scenarios as well.
> Pull Request:  https://github.com/apache/struts/pull/169



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Mime
View raw message