2013-09-05 29 views
2

我試圖編輯表格中的數據(複選框)。 但是,當我在透過按鈕點擊,在控制器的形式爲空使用SpringMVC和Thymeleaf提交時的空表格

我沒有找到一個thymeleaf例如,所以我適應this example

我用一個表單對象:

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> 

我忘了什麼?

+0

找到我認爲你需要「日:值」在你輸入申請和「名」。 此外,我使用標記和一個參數ot此標記是'modelAttribute' – paulek

回答

3

應使用以下語法:

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

,然後使用設置輸入字段:

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

將在您的MyForm的綁定與輸入數據。

更多的例子可以在這裏 Thymeleaf and forms