2015-05-16 40 views
0

我有一个嵌套对象,我将它用作表单的模型。Thymeleaf,Spring嵌套后台对象不绑定表单上的值提交

public AgeBracketSet implements Serializable{ 
    private String id; 
    private List<AgeBracket> ageBrackets; 

    /* Getters and Setters */ 
} 

我已成功绑定该对象的形式的所有属性,并且当视图状态呈现我可以可视化它们的值。这里是我用Thymeleaf做的一个简化版本。本质上,我遍历列表中的项目并获取它们的属性。

<form id="bracketForm" role="form" th:action="${flowExecutionUrl}" th:object="${ageBracketSet}" method="post"> 
    <input th:id="'bracketSet_'+*{id}" th:field="*{id}" /> 
    <th:block th:each="bracket,loop : *{ageBrackets}" th:id="'bracket_'+${bracket.id}"> 
     <input th:id="'fromAge_'+${bracket.id}" th:field="*{ageBrackets[__${loop.index}__].fromAge}" /> 
     <input th:id="'toAge_'+${bracket.id}" th:field="*{ageBrackets[__${loop.index}__].toAge}" /> 
    </th:block> 
</form> 

但是,当我在表单中进行更改并提交时,模型保持不变。我通过调试接收表单数据的服务来确认这一点。该模型没有在表单中进行更改。我在这里做错了什么?

回答

1

我很尴尬地说我找到了解决方案。由于每个输入中缺少'name'属性,因此值不会发布到webflow。使用与名称相同的动态生成的ID作为作业,并且绑定对于列表中的每个项目都是正确的。像这样:

<input th:id="'fromAge_'+${bracket.id}" th:name="'fromAge_'+${bracket.id}" th:field="*{ageBrackets[__${loop.index}__].fromAge}" /> 

感谢大家谁花时间阅读这个愚蠢的职位。下次我会更加小心;)