thymeleaf从具有类似字段名的输入中检索文本,并将其添加到另一个以逗号分隔的输入中

wgxvkvu9  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(203)

我请求帮助是因为thymeleaf做了一件奇怪的事:这是我的表格:

<form action="#" th:action="@{/add-new-board}" method="post">

    <p>Board name: <input type="text" th:name="board" th:field="${board.name}" /></p>
    <p th:if="${#fields.hasErrors('board.name')}" th:errors="${board.name}">Name Error</p>

    <p>Section #1 name: <input type="text" th:name="section" th:field="${section.name}" /></p>
    <p th:if="${#fields.hasErrors('section.name')}" th:errors="${section.name}">Name Error</p>

    <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
</form>

这是我的控制器:

@GetMapping(path = "/add-new-board")
public String addNewBoardForm(Model model) {
    model.addAttribute("board", new Board());
    model.addAttribute("section", new Section());
    return "fragments/forms/add-new-board";
}

@PostMapping(path = "/add-new-board")
public String addNewBoardSubmit(@Valid @ModelAttribute Board board,
                                @Valid @ModelAttribute Membership membership,
                                @Valid @ModelAttribute Section section,
                                @AuthenticationPrincipal UserDetailsImpl principal,
                                BindingResult result,
                                RedirectAttributes attributes) {
    if (result.hasErrors()) {
        attributes.addFlashAttribute("create_board_fail", "Check if you have all fields");
        return "fragments/forms/add-new-board";
    } else {
        board.setCreated_at(LocalDateTime.now());
        Slugify slug = new Slugify();
        board.setSlug(slug.parse(board.getName()));
        boardRepository.save(board);

        User user = userRepository.findByEmail(principal.getEmail()).get();
        membership.setMember_type(MemberType.MANAGER);
        membership.setBoardId(board);
        membership.setUserId(user);
        membershipRepository.save(membership);

        section.setBoard(board);
        section.setColor(ColorType.BLUE_BASIC);
        section.setOrdering(1);
        sectionRepository.save(section);

        attributes.addFlashAttribute("create_board_success", "You successfully added a new board!");
        return "redirect:/";
    }

因此,我的目标是将第一个输入到“board”表的文本插入到“name”列,并将第二个输入到“section”表的文本插入到“name”列。所以这个专栏的标题是相似的。现在,当我运行代码、填充输入并提交它时,我将进入我的数据库:
数据库表img
其中“aaa”是我在第一个输入中写的,而“bbb”是在第二个输入中写的

oymdgrw7

oymdgrw71#

这是因为你正在使用 th:field 不正确。 th:field 设计用于单个 th:object 但现在你用的是两个不同的对象 board 以及 section . 当呈现html时,两个输入可能具有相同的 name="name" 提交时,这些值被连接在一起,您就可以看到所看到的行为。
您应该将board和section添加到单个对象中,并将其用作窗体。例如,如果您创建了 BoardForm 对象:

public class BoardForm {
  private Board board = new Board();
  private Section section = new Section();

  // Getters and setters...
}

将其添加到模型中

model.addAttribute("form", new BoardForm());

那么你的html会是这样的

<form action="#" th:action="@{/add-new-board}" th:object="${form} method="post">
    <p>Board name: <input type="text" th:name="board" th:field="*{board.name}" /></p>
    <p>Section #1 name: <input type="text" th:name="section" th:field="*{section.name}" /></p>
    <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
</form>

相关问题