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

Fixes broken checkbox and checkboxlist implementation.

    XMLWordPrintableJSON

Details

    • Patch

    Description

      Checkbox handling is broken in webwork, since html checkboxes result in no parameter being sent with the form submission. The parameters interceptor has no way to see the missing checkbox fields, and so never unchecks things with "true" as default, and will not empty a list when a checkboxlist with nothing is selected is submitted.

      This fixes the issue.

      This will make your checkboxes and checkbox lists always set the appropriate values on your model, regardless of whether the checkbox is selected or not. Use the interceptor below in place of the default ParametersInterceptor, and insert this line at the beginning of the checkbox.ftl and checkboxlist.ftl templates:

      <input type="hidden" id="WebWork-checkboxExists" name="${parameters.name?html}" value="${parameters.fieldValue?html}"/>

      Here's the interceptor, written as a subclass of ParametersInterceptor. This could be rolled into ParametersInterceptor.

      import com.opensymphony.xwork.interceptor.ParameterNameAware;
      import com.opensymphony.xwork.interceptor.ParametersInterceptor;
      import com.opensymphony.xwork.util.OgnlValueStack;

      import java.util.Iterator;
      import java.util.Map;
      import java.util.TreeMap;

      public class CheckboxParameterInterceptor extends ParametersInterceptor {

      protected void setParameters(Object action, OgnlValueStack stack, final Map parameters) {
      ParameterNameAware parameterNameAware = (action instanceof ParameterNameAware)
      ? (ParameterNameAware) action : null;

      for (Iterator iterator = (new TreeMap(parameters)).entrySet().iterator(); iterator.hasNext() {
      Map.Entry entry = (Map.Entry) iterator.next();
      String name = entry.getKey().toString();

      boolean acceptableName = acceptableName(name)
      && (parameterNameAware == null

      parameterNameAware.acceptableParameterName(name));

      if (acceptableName) {
      Object value = entry.getValue();

      if (name.startsWith("WebWork-checkboxExists")) {
      String[] checkboxes = (String[]) value;
      for (int i = 0; i < checkboxes.length; i++) {
      String checkbox = checkboxes[i];
      if (! parameters.containsKey(checkbox))

      { stack.setValue(checkbox, new String[0]); }

      }
      }

      stack.setValue(name, value);
      }
      }
      }

      }

      Attachments

        Issue Links

          Activity

            People

              mrdon Donald J. Brown
              pmolettiere Peter Molettiere
              Votes:
              1 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: