title: Thymeleaf - how to use Hashmap
body: <p>I'd like to use HashMap as List has been used in following example: <a href="http://www.thymeleaf.org/doc/thymeleafspring.html#dynamic-fields" rel="nofollow">http://www.thymeleaf.org/doc/thymeleafspring.html#dynamic-fields</a>
  I've tried however without effect. </p>
  
  <p>Edit: I'm providing some code. So the problem is that I'd like to use hashmap products in the form, the example I've provided above works great with a list, however I'd like to use it with hashmap.
  Entity:</p>
  
  <pre><code>@Entity
  @Table(name = "meals")
  public class Meal extends BaseEntity{
  @NotEmpty
  private String name;
  @NotEmpty
  private String recipe;
  private String image;
  private double cost;
  @NotNull
  private int kcal;
  @NotNull
  private int proteins;
  @NotNull
  private int fats;
  @NotNull
  private int carbs;
  @NotNull
  private int portions;
  @ElementCollection
  @Column(name = "quantity")
  private Map&lt;Product, Integer&gt; products = new HashMap&lt;&gt;();
  //getters &amp; setters
  </code></pre>
  
  <p>Form:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
  &lt;head th:replace="fragments/headTag :: headTag"/&gt;
  &lt;body&gt;
  &lt;div class="container"&gt;
  &lt;div th:replace="fragments/bodyHeader :: bodyHeader"&gt;&lt;/div&gt;
  &lt;h2 th:text="#{meals.new.title}"/&gt;
  &lt;form th:object="${meal}" th:method="post" th:action="@{${#httpServletRequest.servletPath}}"
        enctype="multipart/form-data"
        class="form-horizontal" id="newMealForm"&gt;
  //other fields
      &lt;div class="control-group" th:classappend="${#fields.hasErrors('products')} ? error"&gt;
          &lt;label class="control-label" th:text="#{meals.products}"/&gt;
  
          &lt;div class="controls"&gt;&lt;span class="help-inline"
                                                                th:errors="*{products}"&gt;[error]&lt;/span&gt;
              &lt;table class="table table-striped"&gt;
                  &lt;thead&gt;
                  &lt;tr&gt;
                      &lt;th th:text="#{product.name}"/&gt;
                      &lt;th th:text="#{product.quantity}"/&gt;
                      &lt;th&gt;&lt;button type="submit" name="addProduct" th:text="#{meals.addProduct}"/&gt;&lt;/th&gt;
                  &lt;/tr&gt;
                  &lt;/thead&gt;
                  &lt;tbody&gt;
                  &lt;tr th:each="products,rowStat : *{products}"&gt;
                      &lt;td&gt;&lt;input type="text" th:field="*{products[__${rowStat.index}__].value}" /&gt;&lt;/td&gt;
                      &lt;td&gt;
                          &lt;input type="number" th:field="*{products}" /&gt;
                      &lt;/td&gt;
                      &lt;td&gt;
                          &lt;button type="submit" name="removeProduct"
                                  th:value="${rowStat.index}" th:text="#{meals.removeProduct}"/&gt;
                      &lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;/tbody&gt;
              &lt;/table&gt;
          &lt;/div&gt;
      &lt;/div&gt;
      &lt;div class="form-actions"&gt;
          &lt;button type="submit" class="btn btn-primary" th:text="#{submit}"/&gt;
      &lt;/div&gt;
   &lt;/form&gt;
   &lt;/div&gt;
   &lt;div th:replace="fragments/footer :: footer"&gt;&lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Controller methods to add/remove row:</p>
  
  <pre><code>@RequestMapping(value="/meals/new", params={"addProduct"})
  public String addProduct(final Meal meal, final BindingResult bindingResult) {
      meal.getProducts().put(new Product(), 1);
      return "/meals/new";
  }
  
  @RequestMapping(value="/meals/new", params={"removeProduct"})
  public String removeRow(
          final Meal meal, final BindingResult bindingResult,
          final HttpServletRequest req) {
      final Integer rowId = Integer.valueOf(req.getParameter("removeProduct"));
      meal.getProducts().remove(rowId.intValue());
      return "/meals/new";
  }
  </code></pre>
  
  <p>Error I've got is:</p>
  
  <pre><code>org.springframework.beans.InvalidPropertyException: Invalid property 'products[0]' of bean class [org.cybuch.incessantfeasting.model.Meal]: Invalid index in property path 'products[0]'; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type 'java.lang.String' to required type 'org.cybuch.incessantfeasting.model.Product' for property 'null'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type org.cybuch.incessantfeasting.model.Product for value '0'; nested exception is java.lang.IllegalArgumentException: Unable to parse '0'
  </code></pre>