I have a html page with list of checkbox. I want to pass values from html to controller using thymeleaf. Here the controller :
@GetMapping(Routes.MarketPlace.MARKETPLACE_DETAIL)
@RolesAllowed({ Roles.CUSTOMER })
public String detailService(@PathVariable("idService") long idService, Model model) {
ItemListGroupModel itemListGroup = new ItemListGroupModel();
model.addAttribute("itemListGroup", itemListGroup);
Service s = service.testService().stream().filter(e -> e.getId() == idService).findFirst().get();
if (null != s) {
model.addAttribute("items", s.getItems());
} else {
model.addAttribute("items", new ArrayList<>());
}
return "marketplace_service_detail";
}
@PostMapping(Routes.MarketPlace.MARKETPLACE_BUY)
@RolesAllowed({ Roles.CUSTOMER })
public String buyProducts(@ModelAttribute("itemListGroup") ItemListGroupModel itemList, Model model) {
// do somethings
return "marketplace";
}
Here the html page:
<form th:action="@{/marketplace/buy}" method="post"
th:object="${itemListGroup}">
<div th:each="item, iter : ${items}">
<input type="checkbox" th:field="*{itemList}" th:value="${item.id}" />
<b th:text="${item.name}"></b>
</div>
<button class="button is-primary"
th:utext="#{pricelist.marketplace.subscription.buy}"></button>
</form>
Here the model :
public class ItemListGroupModel implements Serializable {
private static final long serialVersionUID = 3353484128447961397L;
private List<Item> itemList = new ArrayList<>();
public List<Item> getItemList() {
return itemList;
}
public void setItemList(List<Item> itemList) {
this.itemList = itemList;
}
}
When i try to pass list of checkbox i have this error :
org.springframework.validation.BindException:
org.springframework.validation.BeanPropertyBindingResult: 1 errors Field error in object 'itemListGroup' on field 'itemList': rejected value [0]; codes [typeMismatch.itemListGroup.itemList,typeMismatch.itemList,typeMismatch.java.util.List,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [itemListGroup.itemList,itemList]; arguments []; default message [itemList]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.List' for property 'itemList'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'com.bst.pricelist.data.entity.service.Item' for property 'itemList[0]': no matching editors or conversion strategy found]
at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:157) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE] at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:124) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE] at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:161) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE] at ....
2018-09-04 14:56:23.276 WARN 8184 --- [nio-8080-exec-7] .m.m.a.ExceptionHandlerExceptionResolver : Resolved exception caused by Handler execution: org.springframework.validation.BindException:
org.springframework.validation.BeanPropertyBindingResult: 1 errors Field error in object 'itemListGroup' on field 'itemList': rejected value [0]; codes [typeMismatch.itemListGroup.itemList,typeMismatch.itemList,typeMismatch.java.util.List,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [itemListGroup.itemList,itemList]; arguments []; default message [itemList]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.List' for property 'itemList'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'com.bst.pricelist.data.entity.service.Item' for property 'itemList[0]': no matching editors or conversion strategy found]
Aucun commentaire:
Enregistrer un commentaire