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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With