使用modal删除Spring MVC

时间:2015-03-12 01:04:16

标签: java javascript spring spring-mvc bootstrap-modal

我在使用模态删除项目时遇到问题。我可以删除只使用href获取id并将其传递给控制器​​。但我不知道如何在模态中获取项目的ID并删除它。

要执行的人员和行动清单

<c:forEach var="user" items="${listpersons}">
                <tr>
                    <td><a href="#">${user.username}</a><!--  <span>Clients</span> --> </td>
                    <td><a href="#">${user.email}</a></td>
                    <td>
                        <button class="btn btn-white btn-sm" data-toggle="tooltip" data-placement="top" title="Compose"><a href="#"><i class="fa fa-envelope-o"></i></a></button>
                        <a href="${pageContext.request.contextPath}/edit?id=${user.id}&name=${user.username}&password=${user.password}&email=${user.email}"><i class="fa fa-edit" title="Edit"></i></a></button>
                        <a href="${pageContext.request.contextPath}/delete?id=${user.id}" title="Delete"><i class="fa fa-trash-o"></i></a>
                        <a href="#myModal" role="button" class="btn btn-large btn-primary" data-toggle="modal">Launch Demo Modal</a>
                    </td>
                    <td class="text-right mail-date">Jan 16</td>
                </tr>
 </c:forEach>

这部分是点击pops.up a时的模态按钮,如果我想真正删除项目

<a href="#myModal" role="button" class="btn btn-large btn-primary" data-toggle="modal">Launch Demo Modal</a>

这是我的删除按钮。我可以在这里删除,但我想删除使用模态

<a href="${pageContext.request.contextPath}/delete?id=${user.id}" title="Delete"><i class="fa fa-trash-o"></i></a>

这是我的模态弹出窗口

<div id="myModal" class="modal fade">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">Confirm Delete</h4>
            </div>

            <div class="modal-body">
                <p>Are you sure you want to delete this user? </p>
            </div>
            <div class="modal-footer">

                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <a href="${pageContext.request.contextPath}/delete?id=${user.id}" title="Delete"><i class="fa fa-trash-o"></i></a>
            </div>
        </div>
    </div>
</div>

我不知道如何在模态弹出窗口中传递人物的id。删除不起作用。它会识别任何id

这是控制器

@RequestMapping(value = "/delete", method = RequestMethod.GET)
    public ModelAndView delete(@ModelAttribute("SpringWeb")User user, ModelMap model, HttpServletRequest request)
    {
        try
        {
            UserDao ud = new UserJDBC();
            int id = Integer.parseInt(request.getParameter("id"));
            int delete = ud.deleteUser(id);
            model.addAttribute("message", "User deleted Successfuly");

        }
        catch(Exception e)
        {
            System.out.print(e);
            model.addAttribute("message", "Error occured in deleting user.");
        }

        return new ModelAndView("admin-view-users");
    }

用户未被删除。点击模态上的删除

,我得到这个删除?id = 0

1 个答案:

答案 0 :(得分:3)

使用弹出式按钮的href值附加${user.id},即

#myModal_${user.id}

foreach 循环中添加模式弹出窗口,现在用弹出ID添加${user.id}。 即myModal_${user.id}

用此代码替换你的foreach并删除弹出窗口。

<c:forEach var="user" items="${listpersons}">
<tr>
    <td><a href="#">${user.username}</a><!--  <span>Clients</span> --> </td>
    <td><a href="#">${user.email}</a></td>
    <td>
        <button class="btn btn-white btn-sm" data-toggle="tooltip" data-placement="top" title="Compose"><a href="#"><i class="fa fa-envelope-o"></i></a></button>
        <a href="${pageContext.request.contextPath}/edit?id=${user.id}&name=${user.username}&password=${user.password}&email=${user.email}"><i class="fa fa-edit" title="Edit"></i></a></button>
        <a href="${pageContext.request.contextPath}/delete?id=${user.id}" title="Delete"><i class="fa fa-trash-o"></i></a>
        <a href="#myModal_${user.id}" role="button" class="btn btn-large btn-primary" data-toggle="modal">Launch Demo Modal</a>
    </td>
    <td class="text-right mail-date">Jan 16</td>
</tr>

<div id="myModal_${user.id}" class="modal fade">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">Confirm Delete</h4>
            </div>

            <div class="modal-body">
                <p>Are you sure you want to delete this user? </p>
            </div>
            <div class="modal-footer">

                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <a href="${pageContext.request.contextPath}/delete?id=${user.id}" title="Delete"><i class="fa fa-trash-o"></i>Delete</a>
            </div>
        </div>
    </div>
</div>  
</c:forEach>

注意: - 请不要忘记在foreach中添加模态弹出窗口。