java - 如何将 id 传递给 :value and get data of type List<Ingredient>?

标签 java spring spring-boot thymeleaf

我正在从《Actions 5th,第 3 章,JDBC 中的 Spring》学习 Spring boot。

我有墨西哥卷饼类(class):

@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
    }
}

我在前端使用Thymeleaf,如下所示:

<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>  

还有我的 Controller

@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";
}

关于java - 如何将 id 传递给 :value and get data of type List<Ingredient>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56557832/

相关文章:

java - 单个线程可以在多个进程之间共享吗?如果是的话怎么办?

java - 如何从其他 bean 属性值设置 bean 属性值

java - 使用 Java 的 AWS 资源

amazon-web-services - Spring Boot AWS Elastic Beanstalk - nginx 错误连接被拒绝 http ://127. 0.0.1:5000

Java如何像Linux的命令 "du"一样获取文件的磁盘空间?

java - <beans :beans> and <beans> 是什么意思

java - 如何检查 JSON 是否具有字符串列表中的键

javascript - 如何获取变量作为从 Controller 到 JavaScript 的响应?

java - 将所有请求参数映射到 Spring Controller 中的一个对象中

reactjs - docker环境中React和Spring Boot之间的连接拒绝错误