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

时间:2017-04-07 14:14:14

标签: java spring-boot thymeleaf

我在Thymeleaf中创建了一个动态表单,它在UI上以表格格式填充所有用户的反馈。当控制器的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)时,feedbacklist将变为NULL。有人可以指导我吗?

1 个答案:

答案 0 :(得分:1)

要使用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对象来说明你想要设置值的数组位置,同样作为对象内的常规字段,你必须使用&#39; *&#39;符号