Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thymeleaf multiple select on list on an object

Thymeleaf multiple select on list on an object

I am using Thymeleaf and Spring MVC and I want to do a multiple select like this:

<form action="#" th:object="${promotion}" th:action="@{/promotion/add}" method="post">

<select th:field="*{products.id}" th:value="*{products.id}"  multiple="multiple" >
  <optgroup th:each="c : ${categories}" th:label="${c.name}">   
     <option th:each="p : ${c.products}" th:value="${p.id}" th:text="${p.name}" />
  </optgroup>
</select>
</form>

I have this object in the form

public class Promotion implements Serializable{
    private static final long serialVersionUID = 1L;
    private int id;
    private String name;
    private List<PromoProduct> products;
}

public class PromoProduct implements Serializable {
    private static final long serialVersionUID = 1L;
    private int id;
    private List<Integer> products;
    private int amount;
}

With this code I get the following error message:

org.springframework.web.util.NestedServletException: Request processing failed;
    nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "products.id" .

And I would like to get the selected data on id variable (inside products list). Could I do that? I don't know if this is possible, and if it is how could reach to id variable inside products object?

like image 666
Zuri Avatar asked Nov 08 '22 16:11

Zuri


1 Answers

Shortly - products is the List, so it don't have field id. You need to create the PropertyEditor to realize the behaviour you want.
Nice answer given by @tom-verelst in thymeleaf multiple selected on edit. Please check it.

like image 172
sanluck Avatar answered Nov 14 '22 21:11

sanluck