Collection を Spring MVC のフォームにバインドしようとしてもうまくいかない理由を教えてください。
これが私のオブジェクトの外観です-
public class TestObj {
private Integer testNumber;
private String home;
private String destination;
}
上記のオブジェクトのリストを含む私のフォームオブジェクトは次のとおりです-
public class TestForm {
private List<TestObj> testList;
//contains getter and setter for testList
}
私のコントローラーでは、 formBackingObject メソッドを実装しました -
public class MyController extends SimpleFormController {
public MyController() {
setCommandClass(TestForm.class);
setCommandName("testForm");
}
protected Object formBackingObject(HttpServletRequest request) throws Exception {
if (isFormSubmission(request)) {
testForm = (TestForm) super.formBackingObject(request);
//THIS ALWAYS RETURNS NULL ON FORM SUBMISSION
List<TestObj> testList = testForm.getTestList();
} else {
//load initial data using hibernate. TestObj is hibernate domain object.
List<TestObj> testList = myService.findTestList();
testForm = new TestForm(testList);
}
return testForm;
}
これが私のJSPスニペットです-
<form:form commandName="testForm" method="post">
<c:forEach items="${testForm.testList}" var="testvar" varStatus="testRow">
<tr>
<td>
<form:hidden path="testList[${testRow.index}].home" />
<c:out value="${testvar.home}" />
</td>
<td>
<form:input path="testList[${testRow.index}].destination" />
</td>
</tr>
</c:forEach>
<tr><td><input type="submit"></td></tr>
</form:form>
初めてデータをロードしたときはフォームに問題なく表示されますが、送信ボタンを押すと、コントロールは formBackingObject メソッドに移動し、isFormSubmission は true を返します。ただし、super.formBackingObject(request) を使用してコマンド オブジェクトを取得すると、testList 値が null のフォーム オブジェクトが返されます。この単純なケースが機能しない理由を理解できませんか?
これを機能させるための助けに本当に感謝します。