我试图用百里香的形式获得价值观。表单是一个表,其中有几个表示CallDataRecord的一行。我正在寻找的行为是获取包含每个元素列表的CallDataRecord。
这是我的控制者:
@RequestMapping(value="/usage", method=RequestMethod.POST)
public ModelAndView usageSubmit(@ModelAttribute CallDataRecord cdr, ModelAndView mav) {
System.out.println(cdr.getRecords().size());
return mav;
}
对象来源:
public class CallDataRecord {
private ArrayList<CallDataRecordLine> records;
...
}
public class CallDataRecordLine {
String chargedQuantity;
String fromNumber;
String toNumber;
String dt;
String usage;
}
我的相关jSP
<form action="#" th:action="@{/usage}" th:object="${cdr}" method="post">
<tr id='addr0'>
<th:block th:each="calldatarecordline : ${cdr.records}">
<td>1</td>
<td><input type="text" th:field="${calldatarecordline.fromNumber}" name='fromNumber'
placeholder='+555...' class="form-control" /></td>
<td><input type="text" name='toNumber' th:field="${calldatarecordline.toNumber}" placeholder='+555...'
class="form-control" /></td>
<td>
<div class='input-group date'>
<input name='date' type="text" th:field="${calldatarecordline.dt}" class="form-control"><span
class="input-group-addon"><i
class="glyphicon glyphicon-th"></i></span>
</div>
</td>
<td><input type="text" name='duration' th:field="${calldatarecordline.usage}" class="form-control" /></td>
<td><select class="form-control" name='usageType'>
<option value="value1">Message</option>
<option value="value2">Data</option>
<option value="value3">Call</option>
</select></td>
</th:block>
</tr>
</form>
最后,我获取了我的CallDataRecord,里面的列表是instanciated但是空的所以它没有按预期工作。 我试图在另一个JSP中只检索一个CallDataRecordLine,它工作正常。所以问题是列表上的迭代。
感谢您的时间
答案 0 :(得分:0)
所以有几件事:
将th:field =“$ {}”的所有实例更改为:field =“* {}”并删除calldatarecordline(请查看以下内容)
input type =“text”th:field =“* {fromNumber}”name ='fromNumber'placeholder ='+ 555 ...'class =“form-control”
您已经将对象设置为整个部分的calldatarecordline,无需再次调用它。
编辑1:重写代码以便能够迭代
<form action="#" th:action="@{/usage}" th:object="${cdr}" method="post">
<table id='addr0'>
<tr th:each="calldatarecordline : ${cdr.records}">
<td>1</td>
<td>
<input type="text" th:field="*{fromNumber}" name='fromNumber'
placeholder='+555...' class="form-control"/>
</td>
<td>
<input type="text" name='toNumber' th:field="*{toNumber}"
placeholder='+555...' class="form-control"/>
</td>
<td>
<div class='input-group date'>
<input name='date' type="text" th:field="*{dt}" class="form-control">
<span class="input-group-addon"><i class="glyphicon glyphicon-th"></i></span>
</div>
</td>
<td><input type="text" name='duration' th:field="*{usage}" class="form-control"/></td>
<td>
<select class="form-control" name='usageType'>
<option value="value1">Message</option>
<option value="value2">Data</option>
<option value="value3">Call</option>
</select>
</td>
</tr>
</table>
</form>