[英]How can I collect object using thymeleaf?
PROBLEM:When I submitted the form, the server report:"Failed to convert property value of type java.lang.String[] to required type java.util.List for property ingredients". 问题:当我提交表单时,服务器报告:“无法将类型java.lang.String []的属性值转换为属性成分所需的类型java.util.List”。
MY GUESS:I think the problem is that collected property ingredients is String. 我的猜测:我认为问题在于收集的财产成分是String。 I use thymeleaf as frontend.
我用百里香叶作为前端。
QUESTION:How can I collect ingredients using thymeleaf? 问题:如何使用百里香叶收集成分?
DOMAIN Taco:
private String name;
private List<Ingredient> ingredients;
DOMAIN Ingredient:
private final String id;
private final String name;
private final Type type;
public static enum Type{
WRAP, PROTEIN, VEGGIES, CHEESE, SAUCE
}
CONTROLLER:
import taco.Ingredient.Type;
//method to show design
public String showDesign(Model model) {
List<Ingredient> ingredients = new ArrayList<>();
ingredientRepo.findAll().forEach(i-> ingredients.add(i));
Type[] types = Type.values();
for(Type type:types) {
model.addAttribute(type.toString().toLowerCase(),filterByType(ingr
edients, type));
}
model.addAttribute("taco", new Taco());
return "design";
}
//method that is used to handle the post request
@PostMapping
public String processDesignForm(Taco taco) {
log.info("process taco: "+taco);
return "redirect:/orders/current";
}
DESIGN(use thymeleaf):
<form method="POST" th:object="${taco}">
<div th:each="ingredient : ${wrap}">
<input name="ingredients" type="checkbox"
th:value="${ingredient.id}" />
</div>
</form>
You can do the following, get the ids selected (ingredients) as RequestParam 您可以执行以下操作,将选定的ID(成分)获取为RequestParam
@PostMapping
public String processDesignForm(Taco taco, @RequestParam("ingredients") List<String> idIngredients) {
log.info("process taco: "+taco);
if(idIngredients != null){
for(String id : idIngredients){
...//handle it
}
}
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.