title: spring mvc checking checkbox does not set object
body: <p>Hi I am new to spring MVC and am trying to implement a form:checkboxes tag and have run into a few issues. All the examples I have googled work with Strings and i want to work with objects so am hoping someone can advise. </p>
  
  <p>I have a List of objects set in my DTO as follows:</p>
  
  <pre><code>TestDTO
  
  private List&lt;Barrier&gt; barriers;
  
  public List&lt;Barrier&gt; getBarriers() {
          return barriers;
      }
  
      public void setBarriers(List&lt;Barrier&gt; barriers) {
          this.barriers = barriers;
      }
  </code></pre>
  
  <p>in my controller class I fetch the barrier objects from the database and add them to my DTO which will be passed to the jsp</p>
  
  <pre><code>        savedRecord.setBarriers(dataService.getBarriers());
  
          mav.addObject("testDto", savedRecord);
  </code></pre>
  
  <p>in my JSP I use the form:checkboxes tag as follows:</p>
  
  <pre><code>&lt;form:checkboxes path="barriers" items="${testDto.barriers}" element="label class='block-label'" 
                              itemLabel="barrier"/&gt;
  </code></pre>
  
  <p>I also tried with adding </p>
  
  <pre><code>itemValue="id"
  </code></pre>
  
  <p>but that did not work either</p>
  
  <p>this is wraped in a from element </p>
  
  <pre><code>&lt;form:form method="post" accept-charset="UTF-8" action="${action}"
          onsubmit="return checkAndSend()" id="create"
          novalidate="" modelAttribute="testDto"&gt;
  </code></pre>
  
  <p>So the issues I am having are as follows:</p>
  
  <ol>
  <li>The checkboxes when displayed all seem to be checked. I have implemented a hashcode and equals method on the barrier object but they still all seem to be checked when I want them unchecked.</li>
  </ol>
  
  <p>Barrier.java</p>
  
  <pre><code>@Override
  public int hashCode() {
      final int prime = 31;
      int result = 1;
      result = prime * result + ((barrier == null) ? 0 : barrier.hashCode());
      result = prime * result + ((display == null) ? 0 : display.hashCode());
      result = prime * result + id;
      return result;
  }
  
  @Override
  public boolean equals(Object obj) {
      if (this == obj)
          return true;
      if (obj == null)
          return false;
      if (getClass() != obj.getClass())
          return false;
      Barrier other = (Barrier) obj;
      if (barrier == null) {
          if (other.barrier != null)
              return false;
      } else if (!barrier.equals(other.barrier))
          return false;
      if (display == null) {
          if (other.display != null)
              return false;
      } else if (!display.equals(other.display))
          return false;
      if (id != other.id)
          return false;
      return true;
  }
  </code></pre>
  
  <ol start="2">
  <li>When I click submit and i look at the testDto my barriers object list is null. How do I get the checked boxes that represent objects to be set on my testDto. </li>
  </ol>
  
  <p>Any pointers and advice is appreciated</p>
  
  <p>Thanks</p>
  
  <p>UPDATE:</p>
  
  <p>Thanks for the pointers. I went with the following. your suggestion helped.</p>
  
  <p>I created the folloiwng in my controller</p>
  
  <pre><code>@InitBinder
  public void initBinder(WebDataBinder binder)
  {
      binder.registerCustomEditor(Barrier.class, new BarrierPropertyEditor(barrierService));
  }
  </code></pre>
  
  <p>and then added a class to do the conversion </p>
  
  <pre><code>public class BarrierPropertyEditor extends PropertyEditorSupport {
  
      private BarrierService barrierService;
  
      public BarrierPropertyEditor(BarrierService barrierService) {
          this.barrierService = barrierService;
      }
  
      @Override
      public void setAsText(String text) {
          Barrier b = barrierService.findById(Integer.valueOf(text));
          setValue(b);
      }
  
  }
  </code></pre>
  
  <p>This sets the barrier objects on my DTO. </p>
  
  <p>(Sorry for the caps) IT DOES NOT SOLVE WHY THE CHECKBOXES ARE CHECKED ON INITIAL LOAD. </p>
  
  <p>Any ideas how to set the checkboxes unchecked on intitial load?</p>