簡體   English   中英

使用SpringMVC和Thymeleaf提交時為空表格

[英]Empty form when submitting with SpringMVC and Thymeleaf

我正在嘗試編輯表中的數據(復選框)。 但是當我單擊提交按鈕時,控制器中的表單為空

我沒有找到百里香的例子,所以我改了這個例子

我使用一個表單對象:

public class MyForm {
  private List<MyObj> data;
  // ...
}

具有兩種方法(GET和POST)的控制器:

@RequestMapping(value="/edit/", method = RequestMethod.GET)
public String editGet(Model model) {
  MyForm form = new MyForm(data);
  model.addAttribute("myForm", form);
  return "editPage";
}

@RequestMapping(value="/edit/save", method = RequestMethod.POST)
public String editPost(@ModelAttribute("myForm") MyForm myForm, Model model) {
 // here myForm.getData() is empty
}

和我的HTML:

<form th:action="@{/edit/save}" th:object="${myForm}" method="POST">
  <table>
    <thead>....</thead>
      <tbody th:each="myObj : *{data}" th:remove="tag">
        <tr>
          <td><span th:text="${myObj.name}"></span></td>
          <td><input type="checkbox" th:checked="${myObj.isOK}"/></td>
        </tr>
      </tbody>
  </table>
  <input type="submit">Save</input>
</form>

我忘記了什么?

您應該使用以下語法:

<tr th:each="myObj, rowStat : *{data}">

然后使用設置輸入字段:

th:field="*{data[__${rowStat}.index}__].myField}"

這會將MyForm與輸入數據綁定。

更多的例子可以在這里找到Thymeleaf和形式

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM