1


Im trying to create a loop using th:each on a list object and so creating like 10 forms binding different objects. As far without success.. is it even possible? if not.. do you have an idea how to dynamically bind an object to forms in a similar way for objects in a list? This is what i tried..

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

Thymleaf / 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>
pepote
  • 313
  • 7
  • 21
  • "Im trying to create a loop using th:each on a list object and so creating like 10 forms binding different objects" Huh? – Katie.Sun Dec 15 '18 at 01:55

1 Answers1

1

I'm not sure if that is what you are trying to do but you can submit a list of persons as an object in one form instead of looping through the forms. You just have to create a wrapper class for your list of persons and use that wrapper class object as an object in your form.

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

Check out this post for details on that: How to bind an object list with thymeleaf?

O. Ivancic
  • 11
  • 1