关于java:Thymeleaf返回一个String数组而不是List <Object>

Thymeleaf returning a String array instead of List<Object>

我正在学习如何使用Spring Boot和Thymeleaf。我有一个问题,我在表单列表中向Thymeleaf页面提供了特定对象的列表。当用户选择值并发布结果时,结果是所选对象的字符串,并且与我要在其中存储值的对象不兼容。

这听起来像是一口嘴,所以下面是代码。

我的问题是:是否有任何方法可以确保Thymeleaf返回对象列表?

输入:一个类将一堆配料传递给表单
此类将"成分"类的"列表"传递给表单(此筛选无关紧要-插入一个列表作为model属性的值,且键为成分的类型)

1
2
3
4
5
6
7
8
9
10
11
@GetMapping
public String showDesignForm(Model model) {
    List<Ingredient> ingredients = new ArrayList<>();
    ingredientRepo.findAll().forEach(i -> ingredients.add(i));  
    Type[] types = Ingredient.Type.values();
    for (Type type : types) {
        model.addAttribute(type.toString().toLowerCase(), filterByType (ingredients, type));
    }  
    model.addAttribute("taco", new Taco());
    return"design";
}

Thymeleaf获取该列表并将其显示在复选框中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<!DOCTYPE html>
<html
xmlns:th="http://www.thymeleaf.org">
<head>
Taco Cloud
<link rel="stylesheet" th:href="@{/styles.css}" />
</head>
<body>
Design your taco!
<img th:src="@{/images/TacoCloud.png}"/>
<form method="POST" th:object="${taco}">


Designate your wrap:

<input name="ingredients" type="checkbox" th:value="${ingredient}" />
<span th:text="${ingredient.name}">INGREDIENT</span><br/>



Pick your protein:

<input name="ingredients" type="checkbox" th:value="${ingredient}" />
<span th:text="${ingredient.name}">INGREDIENT</span><br/>



Choose your cheese:

<input name="ingredients" type="checkbox" th:value="${ingredient}" />
<span th:text="${ingredient.name}">INGREDIENT</span><br/>



Determine your veggies:

<input name="ingredients" type="checkbox" th:value="${ingredient}" />
<span th:text="${ingredient.name}">INGREDIENT</span><br/>



Select your sauce:

<input name="ingredients" type="checkbox" th:value="${ingredient}" />
<span th:text="${ingredient.name}">INGREDIENT</span><br/>




Name your taco creation:
<input type="text" th:field="*{name}"/>
<br/>
<button>Submit your taco</button>

</form>
</body>
</html>

目的地类:期望返回的值是List

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package tacos;

import java.util.Date;
import java.util.List;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import lombok.Data;

@Data
public class Taco {

    private Long id;

    private Date createdAt;

    @NotNull
    @Size(min=5, message="Name must be at least 5 characters long")
    private String name;
    @Size(min=1, message="You must choose at least 1 ingredient")
    private List<Ingredient> ingredients;
}

错误:

Field error in object 'taco' on field 'ingredients': rejected value [Ingredient(id=FLTO, name=Flour Tortilla, type=WRAP),Ingredient(id=CHED, name=Cheddar, type=CHEESE)]; codes [typeMismatch.taco.ingredients,typeMismatch.ingredients,typeMismatch.java.util.List,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [taco.ingredients,ingredients]; arguments []; default message [ingredients]]; default message [Failed to convert property value of type 'java.lang.String[]' to required type 'java.util.List' for property 'ingredients'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'tacos.Ingredient' for property 'ingredients[0]': no matching editors or conversion strategy found]


n


我看过一个视频,该视频帮助在Thymeleaf页面的String结果和Spring中的POST方法之间创建了一个转换器。

https://www.youtube.com/watch?v=e9mlrHyn73w


n