java - nested objects in thymeleaf -
i'm trying build form using thymeleaf, entity includes collection (list) of objects. i'm struggling build proper form case.
this entity:
@entity public class owner { @id @generatedvalue private int id; @column(name = "first_name") private string firstname; @column(name = "last_name") private string lastname; @onetomany(cascade = cascadetype.all, mappedby = "owner") private list<phone> phones; @onetomany(cascade = cascadetype.all, mappedby = "owner") private list<pet> pets; //getterss , setters
this pet.class
@entity public class pet { @id @generatedvalue private int id; @column(name = "pet_name") private string petname; @manytoone @joincolumn(name = "owner_id") private owner owner; //getters , setters
and html file building form:
<!doctype html> <html xmlns:th="http:/www.thymealeaf.org"> <head> <meta charset="iso-8859-1"></meta> <title>add new owner database</title> </head> <body> <div id="form"> <form th:action="@{/addowner.do}" th:object="${owner}" method="post"> <label for="firstname">first name: </label> <input type="text" th:field="*{firstname}" /> <label for="lastname">last name:</label> <input type="text" th:field="*{lastname}" /> <!-- here core problem --> <label for="pets">pet name: </label> <input type="text" th:each="pet : *{pets}"/> <input type="submit" value="add" /> </form> </div> </body>
so i'm struggling this:
<!-- here core problem --> <label for="pets">pet name: </label> <input type="text" th:each="pet : *{pets}"/>
my problem is, dont know how access pet object in list collection inside of form make input of petname property.
//edit or maybe there different way this? obvious way getting string form , in controller building string pet name before adding object database?
can try , let me know if worked you
<div th:each="pet, stat : *{pets}"> <input type="text" th:field="*{pet[__${stat.index}__].petname}" /> </div>
Comments
Post a Comment