gpt4 book ai didi

java - 无法使用 Spring Boot 和 Thymeleaf 在动态创建的表单中获取更新的值

转载 作者:行者123 更新时间:2023-12-02 13:28:53 27 4
gpt4 key购买 nike

我在 Thymeleaf 中创建了一个动态表单,它在 UI 上以表格格式填充所有用户的反馈。当 Controller 的 GET Api 被命中时,首先调用该表单。相关代码如下:

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>

基本上我创建了两个 bean,一个是 Feedback.java bean,另一个是 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;
}
}

我的Controller类的相关代码如下:

    @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 )时,反馈列表将为 NULL。有人可以指导我吗?

最佳答案

要在 Thymeleaf 的表单中使用列表有点棘手,您需要使用特定的语法,这里我向您展示一个示例。

<tr th:each="feedback : ${feedbacklist.myfbList}">
<td th:field="*{myfbList[__${feedbackStat.index}__].message}">The
first name
</td>
...//Same for others fields
</tr>

在 thymeleaf 中,您必须使用 Stat 对象来表示要设置值的数组位置,同样作为对象内的普通字段,您必须使用“*”符号。

关于java - 无法使用 Spring Boot 和 Thymeleaf 在动态创建的表单中获取更新的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43280355/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com