title: Thymeleaf - How to POST a fixed object to controller?
body: <p>I have a collection of checkboxes where each one represents a topping of a 'product'. I want to pass a modified 'product' object with the checked 'toppings' stored as 'selectedToppings'.</p>
  
  <p>I've tried the code below but it doesn't work. The problem is that I'm passing an empty 'product' object and only the checkbox values are stored(as 'selectedToppings'). 
  How can I pass a the 'product' object along with the 'selectedToppings'?</p>
  
  <p>html(fragment):</p>
  
  <pre><code>            &lt;form method="post" th:object="${product}" th:action="@{/addItemToCart}"&gt;
              &lt;div class="modal-body"&gt;
                      &lt;div class="card-body"&gt;
                          &lt;div th:each="topping : ${product.toppings}"&gt;
                              &lt;div class="custom-control custom-checkbox my-1 mr-sm-2"&gt;
                                  &lt;input type="checkbox" class="custom-control-input" th:id="${topping} + ${product.id}" name="toppings" th:value="${topping}" /&gt;
                                  &lt;label class="custom-control-label" th:for="${topping} + ${product.id}" th:text="${topping}"&gt;&lt;/label&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;div class="modal-footer"&gt;
                      &lt;button type="submit" class="btn btn-block btn-success"&gt;Add to Shopping Cart&lt;/button&gt;
                  &lt;/div&gt;
              &lt;/form&gt;
  </code></pre>
  
  <p>controller:</p>
  
  <pre><code>@RequestMapping(value="/addItemToCart", method=RequestMethod.POST)
  public String addItemToCart(@ModelAttribute("product") Product product, @RequestParam("toppings") List&lt;String&gt; toppings)
  {
      Product newItem = product;
      newItem.setSelectedToppings(toppings);
      addItemToBucket(newItem);
  
      return "redirect:/menu";
  }
  </code></pre>
  
  <p>entity:</p>
  
  <pre><code>public class Product
  {
  private String id;
  private String name;
  private String description;
  private Double price;
  private List&lt;String&gt; toppings;
  private List&lt;String&gt; selectedToppings;
  
  public Product()
  {
   this.id = "";
   this.name = "";
   this.description = "";
   this.price = 0.0;
   this.toppings = new ArrayList&lt;String&gt;();
   this.selectedToppings = new ArrayList&lt;String&gt;();
  }
  public Product(String id, String name, String description, Double price, List&lt;String&gt; toppings)
  {
      this.id = id;
      this.name = name;
      this.description = description;
      this.price = price;
      this.toppings = toppings;
      this.selectedToppings = new ArrayList&lt;String&gt;();
  }
  //...getters &amp; setters
  }
  </code></pre>