Reputation: 15756
I try to have something like a List as a ModelAttribute with Thymeleaf, but I do not get it to work. I read this page http://www.bincsoft.com/blog/thymeleaf-and-lists-in-forms/ but my code fails.
here are my files:
container class
import java.util.ArrayList;
import java.util.List;
import javax.validation.Valid;
public class WagerateForm {
@Valid
private List<WagerateUi> wagerateuilist;
public WagerateForm() {
wagerateuilist = new ArrayList<WagerateUi>();
}
public WagerateForm(List<WagerateUi> wagerateuilist) {
this.wagerateuilist = wagerateuilist;
}
//getter , setter omitted
}
my DTO for ui
public class WagerateUi {
//getter , setter omitted
private boolean standard;
private Long value;
}
controller
@Named
@RequestMapping("/wagerate")
public class WagerateController {
@RequestMapping(value="")
public String wagerate(@ModelAttribute("wagerateform") WagerateForm wagerateForm,
BindingResult bindingResult) {
List<WagerateUi> wagerateUiList = wagerateForm.getWagerateuilist();
System.out.println(wagerateUiList.size());
//dummy items
for (int i = 0; i < 10; i++) {
WagerateUi temp = new WagerateUi();
temp.setValue(100L);
wagerateUiList.add(temp);
}
return "wagerate";
}
thymeleaf html
<form id="wagerateform" class="form-horizontal" role="form" action="#" th:object="${wagerateform}" th:action="@{/wagerate}" method="post">
<div th:each="wagerateui, iterStat : ${wagerateform.wagerateuilist}">
<input th:field="*{wagerateform[__${iterStat.index}__].value}" class="form-control">
</div>
<button type="submit">Test</button>
</form>
error message
Caused by: org.springframework.beans.NotReadablePropertyException: Invalid property 'wagerateform[0]' of bean class [WagerateForm]: Bean property 'wagerateform[0]' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
at org.springframework.beans.BeanWrapperImpl.getPropertyValue(BeanWrapperImpl.java:725)
at org.springframework.beans.BeanWrapperImpl.getNestedBeanWrapper(BeanWrapperImpl.java:571)
at org.springframework.beans.BeanWrapperImpl.getBeanWrapperForPropertyPath(BeanWrapperImpl.java:548)
at org.springframework.beans.BeanWrapperImpl.getPropertyValue(BeanWrapperImpl.java:714)
at org.springframework.validation.AbstractPropertyBindingResult.getActualFieldValue(AbstractPropertyBindingResult.java:99)
at org.springframework.validation.AbstractBindingResult.getFieldValue(AbstractBindingResult.java:229)
at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:120)
at org.thymeleaf.spring4.util.FieldUtils.getBindStatusFromParsedExpression(FieldUtils.java:348)
at org.thymeleaf.spring4.util.FieldUtils.getBindStatus(FieldUtils.java:288)
at org.thymeleaf.spring4.util.FieldUtils.getBindStatus(FieldUtils.java:260)
The size of the list in the controller is always 0. I cant see whats wrong, i tried different ways, but I think my Thymeleaf/Spring EL is wrong.
Upvotes: 0
Views: 4433
Reputation: 583
Your wagerateform
is not a List, the List is the wagerateuilist
. With the th:each
you fill the wagerateui
object with elements while your looping through your List.
<div th:each="wagerateui : ${wagerateform.wagerateuilist}">
<input th:field="${wagerateui.value}" class="form-control">
</div>
Upvotes: 0
Reputation: 15756
I got it working so far with:
<input th:value="${wagerateform.wagerateuilist[__${iterStat.index}__].value}" class="form-control">
Upvotes: 4