获取百里香叶的物品清单

时间:2015-03-16 15:08:05

标签: forms jsp spring-mvc thymeleaf

我试图用百里香的形式获得价值观。表单是一个表,其中有几个表示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,它工作正常。所以问题是列表上的迭代。

感谢您的时间

1 个答案:

答案 0 :(得分:0)

所以有几件事:

  1. 您正在生成列而不是数据行(只是确保您正在追求的是这些)
  2. 将th:field =“$ {}”的所有实例更改为:field =“* {}”并删除calldatarecordline(请查看以下内容)

    input type =“text”th:field =“* {fromNumber}”name ='fromNumber'placeholder ='+ 555 ...'class =“form-control”

  3. 您已经将对象设置为整个部分的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>