简体   繁体   English

如何使用百里香叶收集物体?

[英]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
         } 
    }
...

如何转换 ArrayList<string> 至 ArrayList<object> using.collect(Collectors.toList() 在 Java stream<div id="text_translate"><p> 如何在 Java stream 中使用.collect(Collectors.toList()将ArrayList&lt;String&gt;转换为ArrayList&lt;Object&gt;</p><p> 之前我用过</p><pre>CommonList = detailUtils.SelectIDValueGetterObservable(getActivity()).stream().forEach(i -&gt; CommonList.add(new foo(i));`</pre><p> 但是我遇到了这些<strong>副作用</strong></p><p> <a href="https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html" rel="nofollow noreferrer">https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html</a></p><p> 这建议.collect(Collectors.toList()</p><p> 我们该怎么做</p></div></object></string> - How can I convert ArrayList<String> to ArrayList<Object> using .collect(Collectors.toList() in Java stream

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何转换 ArrayList<string> 至 ArrayList<object> using.collect(Collectors.toList() 在 Java stream<div id="text_translate"><p> 如何在 Java stream 中使用.collect(Collectors.toList()将ArrayList&lt;String&gt;转换为ArrayList&lt;Object&gt;</p><p> 之前我用过</p><pre>CommonList = detailUtils.SelectIDValueGetterObservable(getActivity()).stream().forEach(i -&gt; CommonList.add(new foo(i));`</pre><p> 但是我遇到了这些<strong>副作用</strong></p><p> <a href="https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html" rel="nofollow noreferrer">https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html</a></p><p> 这建议.collect(Collectors.toList()</p><p> 我们该怎么做</p></div></object></string> - How can I convert ArrayList<String> to ArrayList<Object> using .collect(Collectors.toList() in Java stream 如何将对象传递到百里香模板并访问其属性? - How can I pass object to thymeleaf template and access its attributes? 我们如何使用 thymeleaf 绑定 object 列表的列表 - how we can bind a list of a list of object using thymeleaf 如何使用CrudRepository和百里香叶实现多对多关系? - How can I achieve a manytoone relation using the CrudRepository and thymeleaf? 使用Spring和Thymeleaf无法在视图上传输对象 - Can not transfer object on view using Spring and Thymeleaf 我如何在百里香中显示对象 - How do i display object in thymeleaf 如何使用百里香在html中传递对象 - how to pass the object in html using thymeleaf 如何使用 Thymeleaf 写入响应标头 - How Can I Write to response headers with Thymeleaf 在Thymeleaf中,如何基于当前模型对象为HTML选择器选择th:selected值? - In Thymeleaf, how can I choose the th:selected value for an HTML selector based on the current model object? 我如何收集数组中的所有项目 - How can i collect all items of an array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM