0
我在Thymeleaf中創建了一個動態表單,它在UI上以表格格式填充所有用戶的反饋。當控制器的GET Api被擊中時,表單首先被調用。對於相同的相關代碼如下:無法使用Spring Boot和Thymeleaf在動態創建的表單中獲取更新值
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>
基本上我已經創建了兩個豆,一個是Feedback.java豆,而另一種是FeedbackList.java bean。對於相同的代碼下面給出:
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;
}
}
從我的控制器類
相關代碼如下:
@RequestMapping(value = "/getAll", method = RequestMethod.GET)
public String getAllFeedbacks(@Valid FeedbackList feedbacklist,
BindingResult bindingResult, Model model) {
ArrayList<Feedback> fbarray = new ArrayList<>();
for (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 is coming as NULL below
for (Feedback fb : feedbacklist.getMyfbList()) {
System.out.println(fb.getComment());
System.out.println(fb.getMessage());
System.out.println(fb.getStatus());
}
// Code to update the database with the new status and comment would go
// here
return "result";
}
當我觸發Get請求時,表單在UI上正確呈現,但是,當我在表單中進行一些更改並提交它(POST)時,feedbacklist將作爲NULL傳入。任何人都可以請用這個指導我嗎?
嗨@cralfaro,謝謝你的回覆。實際上,我在代碼中犯了不少錯誤,首先我創建了另一個名爲FeedbackPOJO.java的類,它是一個簡單的POJO類,具有與Feedback.java中相同的字段。此外,我的allfeedbacks.html是相當錯誤的。我遵循http://stackoverflow.com/a/36522177/5105263給出的答案,它幫助我解決了大約90%的問題。我還沒有想出一種方法來正確整合下拉菜單,並會在幾天後發佈更新後的代碼。謝謝 !! –
@SaurabhGourabh當你提出下一個問題時,請給我一個信息,我會看看我是否可以幫助 – cralfaro
當然..謝謝! –