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

Checkbox with multiple values do not default correctly

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Minor
    • Resolution: Not A Problem
    • 2.5.25
    • 6.1.1
    • Core Tags

    Description

      Using multiple s:checkbox (with the same name) together with a List (of Integer or String, etc) does not preselect the right checkboxes. This behaviour is inconsistent with all the other form components that get the default values based on their name. I have constructed a simple example for illustrating and testing. My actual case is more complex with dynamic data. 

      Action class:

      public class TestAction extends ActionSupport {
       private List<Integer> number;
       
      public List<Integer> getNumber() {
         return number;
        }
       
      public void setNumber(List<Integer> number) {
         this.number = number;
       }
      
       public String execute() {
         //Try with some default, empty list, null
         if (number == null) number = Collections.singletonList(2);
         return SUCCESS;
        }
      }
      

      JSP:

      <%@ taglib prefix="s" uri="/struts-tags_rt" %>
      <html>
      <body>
      <s:form action="test">
       1 <s:checkbox name="number" fieldValue="1"/>
       2 <s:checkbox name="number" fieldValue="2"/>
       3 <s:checkbox name="number" fieldValue="3"/>
       4 <s:checkbox name="number" fieldValue="4"/>
       <button type="submit">Push</button>
      </s:form>
      </body>
      </html>
      

      Action configuration:

      <action name="test" class="TestAction" method="execute">
        <result name="success">test.jsp</result>
      </action>
      

      One would expect that the checkboxes should be checked if the corresponding value is in  the list. But instead all checkboxes get checked if the list is not null (independent of its content). And all checkboxes are unchecked if the list is null. Submitting the form after selecting some checkboxes does send the values correctly to the action, but once again when rendering the checkboxes, they all get checked if the list is not null.

      I can simply fix this by adding the value attribute with an expression to each checkbox, but it feels like this should be the default behaviour:

      1 <s:checkbox name="number" fieldValue="1" value="number.contains(1)"/>
      2 <s:checkbox name="number" fieldValue="2" value="number.contains(2)"/>
      3 <s:checkbox name="number" fieldValue="3" value="number.contains(3)"/>
      4 <s:checkbox name="number" fieldValue="4" value="number.contains(4)"/>
      

      Another workaround is to use a Map<Integer, Boolean> and in that case all checkboxes are checked and unchecked correctly, but it feels unnecessary since every entry will always have value true and the only interesting part of the map will be the keySet (which is essentially the list). But it is interesting that the checkboxes behave as expected when using a Map, but not when using a List.

      I am not really sure if this relates to WW-4865

      Attachments

        Activity

          People

            Unassigned Unassigned
            nikos nikos dimitrakas
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: