问题:提交表单时,服务器报告:“无法将类型java.lang.String []的属性值转换为属性成分的必需类型java.util.List”。
我的GUESS:我认为问题在于收集的财产成分是String。 我用百里香叶作为前端。
问题:如何使用百里香叶收集成分?
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>
答案 0 :(得分:0)
您可以执行以下操作,将选定的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
}
}
...