java - Unable to get updated values in a dynamically created form using Spring Boot and Thymeleaf -
i have created dynamic form in thymeleaf populates feedbacks users in table format on ui. form first called when api of controller gets hit. relevant code same given below :
allfeedbacks.html
<h2>dynamic form</h2> <form action="#" th:action="@{/updatefb}" th:object="${feedbacklist}" method="post"> <table> <tr> <th>message</th> <th>status</th> <th>comments</th> </tr> <tr th:each="feedback : ${feedbacklist.myfblist}"> <td th:text="${feedback.message}" th:field="${feedback.message}">the first name</td> <td><select> <option value="pending" th:selected="${feedback.status == 'pending'}">pending</option> <option value="in process" th:selected="${feedback.status == 'in process'}">in process</option> <option value="done" th:selected="${feedback.status == 'done'}">done</option> </select></td> <td><input type="text" placeholder="enter comment here" name="comments" th:text="${feedback.comment}" th:field="${feedback.comment}" /></td> </tr> </table> <button type="submit">submit</button> </form>
basically have created 2 beans, 1 feedback.java bean while other feedbacklist.java bean. code same given below :
feedback.java
@entity @table(name = "feedback") public class feedback implements serializable { private static final long serialversionuid = -3009157732242241606l; @id private string id; public string getid() { return id; } public string getmessage() { return message; } public string getstatus() { return status; } public string getcomment() { return comment; } @column(name = "message") private string message; @column(name = "status") private string status; @column(name = "comment") private string comment; public feedback() { } public feedback(string message, string status) { this.message = message; this.status = status; this.id = uuid.randomuuid().tostring(); }
feedbacklist.java
public class feedbacklist { arraylist<feedback> myfblist; public arraylist<feedback> getmyfblist() { return myfblist; } public void setmyfblist(arraylist<feedback> myfblist) { this.myfblist = myfblist; } }
relevant code controller class follows :
@requestmapping(value = "/getall", method = requestmethod.get) public string getallfeedbacks(@valid feedbacklist feedbacklist, bindingresult bindingresult, model model) { arraylist<feedback> fbarray = new arraylist<>(); (feedback fb : repository.findall()) { fbarray.add(fb); } feedbacklist.setmyfblist(fbarray); model.addattribute("feedback", new feedback()); model.addattribute("feedbacklist", feedbacklist); return "allfeedbacks"; } @requestmapping(value = "/updatefb", method = requestmethod.post) public string updatefbstatus(@valid feedbacklist feedbacklist, bindingresult bindingresult, model model) { //feedbacklist coming null below (feedback fb : feedbacklist.getmyfblist()) { system.out.println(fb.getcomment()); system.out.println(fb.getmessage()); system.out.println(fb.getstatus()); } // code update database new status , comment go // here return "result"; }
the form getting rendered on ui when fire request, however, when make changes in form , submit ( post ), feedbacklist coming null. please guide me ?
to use list inside form thymeleaf little bit more tricky, need use specific syntax, here show example.
<tr th:each="feedback : ${feedbacklist.myfblist}"> <td th:field="*{myfblist[__${feedbackstat.index}__].message}">the first name </td> ...//same others fields </tr>
in thymeleaf have use stat object array position want set value, normal fields inside object have use '*' notation.
Comments
Post a Comment