【问题标题】:How to Post ArrayList of other Entity in Spring with Thymeleaf如何使用 Thymeleaf 在 Spring 中发布其他实体的 ArrayList
【发布时间】:2021-04-15 04:45:51
【问题描述】:

我有一个班级帖子

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToMany
@JoinTable(name = "post_category",
        joinColumns = @JoinColumn(name = "post_id"),
        inverseJoinColumns = @JoinColumn(name = "category_id"))
private List<Category> categories = new ArrayList<Category>();

我还有其他字符串字段,但这并不重要。类别是:

    @Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToMany(mappedBy = "categories")
private List<Post> posts = new ArrayList<>();

我想发布一个 Post 实体。但是我在 Post 类中的 Category 字段有问题。

          <div th:field="${post.categories}">
              <div th:each="category,iter : ${categories}">
                  <input type="checkbox" id=${iter.index} name=${"category"+"["+iter.index+"]"} value=${category.text}>
                  <label for=${iter.index}> The category</label><br>
              </div>
          </div>

我想在后表单 html 中有一个带有类别的复选框列表。如果单击(选中)该类别,我希望将其添加到 Post Class 中的类别列表中。然而它不会发生。在控制器类中,当我在提交后返回 Post 类时,我什至在表单的字段中都没有类别。这些是我在 Controller 中的请求:

@RequestMapping("/posts/new")
public String newPost(Model model){
    model.addAttribute("post", new PostForm());
    model.addAttribute("categories", categoryRepository.findAll());
    return "postForm";
}

@PostMapping
@RequestMapping("post")
public String saveOrUpdate(@ModelAttribute PostForm postForm){
    PostForm savedPost = postService.savePostForm(postForm);

    return "redirect:/post/show/" + savedPost.getId();
}

有人知道如何制作一个复选框列表以单击然后将类别添加到 Post 实体吗? 谢谢

【问题讨论】:

    标签: java html spring spring-boot spring-mvc


    【解决方案1】:

    起初我不确定是否需要这样做

    <div th:field="${post.categories}">
    

    我认为你可以用这样的简单 div 替换它

        <form method="post" th:action="@{/post}">
            <div>
                <div th:each="category,iter : ${categories}">
                    <input type="checkbox" id=${iter.index} name="categories" th:value="${category.text}">
                    <label for=${iter.index}> The category</label><br>
                </div>
            </div>
            <input type="submit">
        </form>
    

    其次,我认为您需要值中的th: 前缀来获取text 属性的值。

    正如您现在所拥有的,您正在尝试在复选框名称中添加索引。我建议将所有复选框命名为与上面相同的名称,因此在 post 控制器上,您将获得一个包含所选项目的字符串数组

        @RequestMapping("/posts/new")
        public String newPost(Model model) {
            model.addAttribute("categories",
                    Arrays.asList(
                            new Category("Category 1"),
                            new Category("Category 2"),
                            new Category("Category 3")));
            return "postForm";
        }
    
        @PostMapping
        @RequestMapping("post")
        public String saveOrUpdate(@RequestParam List<String> categories){
            categories.stream().forEach(System.out::println);
            return "postForm";
        }
    
    

    【讨论】:

      猜你喜欢
      • 2018-05-05
      • 1970-01-01
      • 2017-10-18
      • 2014-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-19
      相关资源
      最近更新 更多