如何将 id 传递给 th:value 并获取 List 类型的数据<Ingredient>?



我正在学习 Spring 启动 从 Spring 在 Actions 5th, Chapter 3, JDBC.

我有炸玉米饼课:

@Data
public class Taco {
    private Long id;
    private Date createdAt;
    @NotBlank
    @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")
    @NotNull(message = "You must choose at least 1 ingredient")
    private List<Ingredient> ingredients;   
}

和成分类别:

@Data
@AllArgsConstructor
public class Ingredient {
    private String id;
    private String name;
    private Type type;
    public static enum Type{
        WRAP, PROTEIN, VEGGIES, CHEESE, SAUCE
    }
}

我在前端使用百里香叶,像这样:

<form method="POST" th:object="${design}">
    <span class="validationError"
              th:if="${#fields.hasErrors('ingredients')}"
              th:errors="*{ingredients}">Ingredients Error</span>                                                
    <div class="grid">
        <!-- WRAP -->
        <div class="ingredient-group" id="wraps">
            <h3>Designate your WRAP:</h3>
            <div th:each="ingredient : ${wrap}" >
                <input name="ingredients" type="checkbox" th:value="${{ingredient.id}}" />
                <span th:text="${ingredient.name}">INGREDIENT</span>
            </div>
        </div>
        <!-- PROTEIN -->
        <div class="ingredient-group" id="proteins">
            <h3>Designate your PROTEIN:</h3>
            <div th:each="ingredient : ${protein}" >
                <input name="ingredients" type="checkbox" th:value="${{ingredient.id}}" />
                <span th:text="${ingredient.name}">INGREDIENT</span>
            </div>
        </div>  

还有我的控制器

@GetMapping
public String showDesignForm(Model theModel) {
    List<Ingredient> ingredients = new ArrayList<>();
    ingredientRepository.findAll().forEach(i -> ingredients.add(i));
    Type[] types = Ingredient.Type.values();
    for(Type type : types) {
        theModel.addAttribute(
                type.toString().toLowerCase(), 
                filterByType(ingredients, type));
    }
    theModel.addAttribute("design", new Taco());
    return "design";
}

我的JdbcIngredientRepository

@Repository
public class JdbcIngredientRepository implements IngredientRepository, RowMapper<Ingredient>{
    private JdbcTemplate jdbc;
    @Autowired
    public JdbcIngredientRepository(JdbcTemplate jdbc) {
        this.jdbc = jdbc;
    }
    @Override
    public Iterable<Ingredient> findAll() {
        String sqlQuery = "select id, name, type from Ingredient";
        return jdbc.query(sqlQuery, this::mapRow);
    }

问题是当我提交时,我收到错误消息:Can't convert from List<String> to List<Ingredient>

我可以通过将private List<Ingredient> ingredients;更改为private List<String> ingredients;来解决此问题。但我认为这是一种不好的做法?任何人都有更好的方法直接从 th:value 中选择的数据传递到 List ?

您可以通过映射 ingredientRepository.findAll() 的结果将存储库中的字符串转换为成分。

@GetMapping
public String showDesignForm(Model theModel) {
    List<Ingredient> ingredients = 
        ingredientRepository.findAll()
            .stream()
            .map(ingredientName -> new Ingredient(ingredientName))
            .collect(Collectors.toList());
    Type[] types = Ingredient.Type.values();
    for(Type type : types) {
        theModel.addAttribute(
                type.toString().toLowerCase(), 
                filterByType(ingredients, type));
    }
    theModel.addAttribute("design", new Taco());
    return "design";
}