Spring MVC 使用模态删除

Spring MVC delete using 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 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");
    }

用户没有被删除。单击模式

上的删除时,我得到这个 delete?id=0

附加 ${user.id} 弹出显示按钮的 href 值,即

#myModal_${user.id}

foreach 循环中添加模态弹出窗口,现在附加 ${user.id} 弹出窗口 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 中添加模态弹出窗口。