title: How to correctly bind checkbox to the object list in thymeleaf?
body: <p>My domain model consist of an <code>Employee</code> and <code>Certificate</code>. One Employee can reference/have many certificates (one-to-many relationship). The full list of certificates could be get from the <code>certificateService</code>. </p>
  
  <p>To assign some special certificate to the employee I used <code>th:checkbox</code> element from thymeleaf as follow: </p>
  
  <pre><code>&lt;form action="#" th:action="@{/employee/add}" th:object="${employee}" method="post"&gt;
      &lt;table&gt;
          &lt;tr&gt;
              &lt;td&gt;Name&lt;/td&gt;
              &lt;td&gt;&lt;input type="text" th:field="*{name}"&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;Certificate&lt;/td&gt;
              &lt;td&gt;
                  &lt;th:block th:each="certificate , stat : ${certificates}"&gt;
                      &lt;input type="checkbox" th:field="*{certificates}" name="certificates" th:value="${certificate.id]}"/&gt;
                      &lt;label th:text="${certificate.name}" &gt;&lt;/label&gt;
                  &lt;/th:block&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td colspan="2"&gt;&lt;input type="submit" value="Add"/&gt;&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/table&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>Now when I'm trying to submit the HTML form I always get following error:</p>
  
  <blockquote>
    <p>400 - The request sent by the client was syntactically incorrect.</p>
  </blockquote>
  
  <p>My question is: How to correctly bind checkbox elements to the object list with thymeleaf?</p>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>@RequestMapping(value = "/add" , method = RequestMethod.GET)
  public String add(Model model) {
      model.addAttribute("employee",new Employee());
      model.addAttribute("certificates",certificateService.getList());
      return "add";
  }
  
  @RequestMapping(value = "/add" , method = RequestMethod.POST)
  public String addSave(@ModelAttribute("employee")Employee employee) {
      System.out.println(employee);
      return "list";
  }
  </code></pre>
  
  <p><strong>Employee Entity</strong></p>
  
  <pre><code>@Entity
  @Table(name = "employee")
  public class Employee {
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Column(name = "ID")
      private int id;
  
      @Column(name = "Name")
      private String name;
  
      @ManyToMany(fetch = FetchType.EAGER)
      @JoinTable(name = "emp_cert",
              joinColumns = {@JoinColumn(name = "employee_id")},
              inverseJoinColumns = {@JoinColumn(name = "certificate_id")})
      private List&lt;Certificate&gt; certificates;
  
      public Employee() {
          if (certificates == null)
              certificates = new ArrayList&lt;&gt;();
      }
  
      public int getId() {
          return id;
      }
  
      public void setId(int id) {
          this.id = id;
      }
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      public List&lt;Certificate&gt; getCertificates() {
          return certificates;
      }
  
      public void setCertificates(List&lt;Certificate&gt; certificates) {
          this.certificates = certificates;
      }
  
      @Override
      public String toString() {
          return "Employee [id=" + id + ", name=" + name + "certificates size = " + certificates.size() + " ]";
      }
  }
  </code></pre>
  
  <p><strong>Certificate Entity</strong></p>
  
  <pre><code>@Entity
  @Table(name = "certificate")
  public class Certificate {
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Column(name = "Id")
      private int id;
  
      @Column(name = "name")
      private String name;
  
      @ManyToMany(mappedBy = "certificates")
      private List&lt;Employee&gt; employees;
  
      public Certificate() {
          if (employees == null)
              employees = new ArrayList&lt;&gt;();
      }
  
      public int getId() {
          return id;
      }
  
      public void setId(int id) {
          this.id = id;
      }
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      public List&lt;Employee&gt; getEmployees() {
          return employees;
      }
  
      public void setEmployees(List&lt;Employee&gt; employees) {
          this.employees = employees;
      }
  
      @Override
      public int hashCode() {
          final int prime = 31;
          int result = 1;
          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;
          Certificate other = (Certificate) obj;
          if (id != other.id)
              return false;
          return true;
      }
  }
  </code></pre>