Uploaded image for project: 'Struts 2'
  1. Struts 2
  2. WW-1534

The value of checkbox getted in server-side is "false" when no any checkbox been selected.

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Major
    • Resolution: Won't Do
    • Affects Version/s: 2.0.1
    • Fix Version/s: 2.5.12
    • Component/s: Value Stack
    • Labels:
      None
    • Environment:

      tomcat-5.5.20 / jdk1.5.0.08

      Description

      jsp section:

      <s:iterator value="%{#request.rolePs.items}" id="role" status="status">
      
         <tr >
                      <td height="25" width="5%">
                          <s:checkbox name="roleId" fieldValue="%{id}" theme="simple"/>
                      </td>
         </tr>
      ........
      </s:iterator>
      

      action section:

      ....
             private String[] roleId;
      	public String[] getRoleId() {
      		return roleId;
      	}
      
      	public void setRoleId(String[] roleId) {
      		this.roleId = roleId;
      	}
      .....
      

      the retrieved value of "roleId" is false when user doesn't select any checkbox elements. In my opinion, this case should returns null directly.

        Issue Links

          Activity

          Hide
          mrdon Don Brown added a comment -

          Setting the property to null makes sense in this situation, but the more common one is where the property is a boolean. Any idea how we could construct a value that OGNL would treat as either null if the value is a string or false if a boolean?

          Show
          mrdon Don Brown added a comment - Setting the property to null makes sense in this situation, but the more common one is where the property is a boolean. Any idea how we could construct a value that OGNL would treat as either null if the value is a string or false if a boolean?
          Hide
          lukaszlenart Lukasz Lenart added a comment -

          Isn't this solved by WW-2339 ?

          Show
          lukaszlenart Lukasz Lenart added a comment - Isn't this solved by WW-2339 ?
          Hide
          mars_pb Markus Schulz added a comment - - edited

          No, this isn't solved by WW-2339. I guess the patch rev655721 mentioned in WW-2339 is now the git commit 9a22985c1f6554b908df14e53cda32b41f1e33ec.

          This patch does't distinguish between a single rendered checkbox with a certain name (often used with boolean getter/setter in the action) and multiple rendered checkboxes with the same name (often used with a list getter/setter (e.g. ids) in the action), but the latter actually renders a single checkbox only.

          This is the case if you e.g. iterate over a list and render a checkbox for every entry in the list. If the list contains only a single entry, then you will get a single checkbox.

          As a workaround a hidden div containing two additional checkboxes can be used to ensure that there are more than one rendered checkboxes:

          <div style="display:none;">
            <s:checkbox name="myIds" fieldValue="0" value="false"/>
            <s:checkbox name="myIds" fieldValue="0" value="false"/>
          </div>
          <s:iterator ...>
            <s:checkbox name="myIds" fieldValue="..." value="..."/>
          </s:iterator>
          

          I know that this is ugly, but I don't see a chance to get this distinguished in the interceptor.

          Show
          mars_pb Markus Schulz added a comment - - edited No, this isn't solved by WW-2339 . I guess the patch rev655721 mentioned in WW-2339 is now the git commit 9a22985c1f6554b908df14e53cda32b41f1e33ec. This patch does't distinguish between a single rendered checkbox with a certain name (often used with boolean getter/setter in the action) and multiple rendered checkboxes with the same name (often used with a list getter/setter (e.g. ids) in the action), but the latter actually renders a single checkbox only. This is the case if you e.g. iterate over a list and render a checkbox for every entry in the list. If the list contains only a single entry, then you will get a single checkbox. As a workaround a hidden div containing two additional checkboxes can be used to ensure that there are more than one rendered checkboxes: <div style= "display:none;" > <s:checkbox name= "myIds" fieldValue= "0" value= "false" /> <s:checkbox name= "myIds" fieldValue= "0" value= "false" /> </div> <s:iterator ...> <s:checkbox name= "myIds" fieldValue= "..." value= "..." /> </s:iterator> I know that this is ugly, but I don't see a chance to get this distinguished in the interceptor.
          Hide
          lukaszlenart Lukasz Lenart added a comment -

          I think this isn't solvable with single <s:checkbox/>, when you want to render a list of checkboxes you should use <s:checkboxlist/> which works pretty well. I wonder what will happen if a list-with-one-element will be used to render <s:checkboxlist/>. Need to check that

          Show
          lukaszlenart Lukasz Lenart added a comment - I think this isn't solvable with single <s:checkbox/> , when you want to render a list of checkboxes you should use <s:checkboxlist/> which works pretty well. I wonder what will happen if a list-with-one-element will be used to render <s:checkboxlist/> . Need to check that
          Hide
          lukaszlenart Lukasz Lenart added a comment -

          Looks like the <s:checkboxlist/> is the solution, I have got null when no selection or [false] when selected the only option.

          Show
          lukaszlenart Lukasz Lenart added a comment - Looks like the <s:checkboxlist/> is the solution, I have got null when no selection or [false] when selected the only option.
          Hide
          mars_pb Markus Schulz added a comment -

          I don't think that <s:checkboxlist/> is the solution. In relation to the description of this issue, assume you like to create a table with more than one columns. One column shows the name of the role and a second one shows a checkbox to delete roles (carrying the roleid as in the description). This is not possible with <s:checkboxlist/> or a combination of <s:iterator ...> and <s:checkboxlist/>, or am I missing something?

          Show
          mars_pb Markus Schulz added a comment - I don't think that <s:checkboxlist/> is the solution. In relation to the description of this issue, assume you like to create a table with more than one columns. One column shows the name of the role and a second one shows a checkbox to delete roles (carrying the roleid as in the description). This is not possible with <s:checkboxlist/> or a combination of <s:iterator ...> and <s:checkboxlist/> , or am I missing something?
          Hide
          aleksandr-m Aleksandr Mashchenko added a comment -

          The `uncheckedValue` in CheckboxInterceptor can be used to set the default value. But I don't think it can be set to null right now.

          Markus Schulz Another hack is to add just hidden fields -> Single checkbox generated with iterator not working while it is not checked

          Show
          aleksandr-m Aleksandr Mashchenko added a comment - The `uncheckedValue` in CheckboxInterceptor can be used to set the default value. But I don't think it can be set to null right now. Markus Schulz Another hack is to add just hidden fields -> Single checkbox generated with iterator not working while it is not checked
          Hide
          lukaszlenart Lukasz Lenart added a comment -

          Markus Schulz the problem here is that the <s:checkbox/> doesn't know that it's inside a <s:iterator/>'s loop and the list has only one element, so it should generate an additional hidden input. We can introduce a hack to detect such corner case but I'm not sure if this a valid solution.

          Show
          lukaszlenart Lukasz Lenart added a comment - Markus Schulz the problem here is that the <s:checkbox/> doesn't know that it's inside a <s:iterator/> 's loop and the list has only one element, so it should generate an additional hidden input. We can introduce a hack to detect such corner case but I'm not sure if this a valid solution.
          Hide
          lukaszlenart Lukasz Lenart added a comment -

          Feel free to re-open when you will have a suggestion how to fix that.

          Show
          lukaszlenart Lukasz Lenart added a comment - Feel free to re-open when you will have a suggestion how to fix that.

            People

            • Assignee:
              lukaszlenart Lukasz Lenart
              Reporter:
              changming Ling Chang Ming
            • Votes:
              2 Vote for this issue
              Watchers:
              7 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development