代码之家  ›  专栏  ›  技术社区  ›  pepote

如何使用th:each将对象绑定到窗体(th:object)中,以获得一个包含对象的对象列表

  •  0
  • pepote  · 技术社区  · 6 年前


    这就是我试过的。。

    @RequestMapping(value = "/area")
        public String index(@AuthenticationPrincipal User currentUser, Model model) {
    
            /* getPersons() returns an object list of diferent persons */
            model.addAttribute("personslist", currentUser.getPersons());
    
            return "area";
        }
    

    文件页/html:

    <div th:each="person: ${personslist}">           
         <form th:object="${person}" th:action="@{/fooBar}" method="post">
              <input hidden="hidden" th:field="${person.id}"/>
                //Other input fields...
               <button type="submit"></button>
         </form>
    </div>
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   O. Ivancic    6 年前

    我不确定这是否是你正在尝试做的,但你可以提交一个列表的人作为一个对象在一个形式,而不是通过循环的形式。您只需为人员列表创建一个包装类,并将该包装类对象用作表单中的对象。

        <form th:object="${personslistWrapper}" th:action="@{/fooBar}" method="post">
    
                // Looping through the persons on the list for each input field
    
               <button type="submit"></button>
         </form>
    

    How to bind an object list with thymeleaf?