繁体   English   中英

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

[英]How can I collect object using thymeleaf?

问题:当我提交表单时,服务器报告:“无法将类型java.lang.String []的属性值转换为属性成分所需的类型java.util.List”。

我的猜测:我认为问题在于收集的财产成分是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>

您可以执行以下操作,将选定的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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM