Thymeleaf表单从每个列表获取错误的属性

时间:2015-07-13 00:27:17

标签: java spring thymeleaf

我遇到$ {ad.id}属性的问题 - 每一行都有一些id值。对于第一行,一切正常,但对于下一行,只有删除按钮发送正确的id值。当我单击编辑按钮(任何行)并弹出模态窗体时,它将获取第一行的id值。我不知道为什么会这样,我会感激任何帮助。

http://i.stack.imgur.com/ODhwE.png

<div class="container">
<h2>Striped Rows</h2>
<p>The .table-striped class adds zebra-stripes to a table:</p>
<table class="table table-striped">
    <thead>
    <tr>
        <th>Id</th>
        <th>Title</th>
        <th>Edit</th>
        <th>Delete</th>
    </tr>
    </thead>
    <tbody>
    <tr  th:each="ad : ${allads}">
        <td th:text="${ad.id}">John</td>
        <td th:text="${ad.name}">Doe</td>
        <td>

            <!--MODAL-->

            <button type="button" class="btn btn-info" data-toggle="modal" data-target="#myModal">
                edit
            </button>

            <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
                <div class="modal-dialog" role="document">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                            <h4 class="modal-title" id="myModalLabel">Edit</h4>
                        </div>
                        <div class="modal-body">

                            <!--FORM-->

                            <form class="form-horizontal" role="form" action="#"
                                  th:action="@{editAd}" th:object="${adEdit}" method="post">
                                <div class="form-group">
                                    <label class="control-label col-sm-2" for="title">Title:</label>
                                    <div class="col-sm-10" >
                                        <input type="text" th:field="*{name}"  class="form-control" id="title" />
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="control-label col-sm-2" for="pwd">Text:</label>
                                    <div class="col-sm-10">
                                        <textarea id="pwd" th:field="*{text}" rows="10" cols="55" th:text="${ad.text}"></textarea>
                                    </div>
                                </div>

                                <div class="form-group">
                                    <div class="col-sm-offset-2 col-sm-10">
                                        <input type="hidden" th:value="${ad.id}" th:name="idEdit"/>
                                        <button type="submit" class="btn btn-default">Submit</button>
                                    </div>
                                </div>
                            </form>



                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                            <button type="button" class="btn btn-primary">Save changes</button>
                        </div>
                    </div>
                </div>
            </div>


        </td>

        <!--DELETE-->

        <td>
            <form method="POST" action="#" th:action="@{deleteAd}">
                <input type="hidden" th:value="${ad.id}" th:name="idDelete"/>
                <input class="btn btn-danger" name="submit" type="submit" value="delete"/>
            </form>
        </td>

    </tr>

    </tbody>
</table>

0 个答案:

没有答案