1

我有一个调用 MySQL 数据库的 servlet。

deletePage方法如下所示:

public void  deletePage(PageData delete) {
    String firstQuery ="Delete FROM pages Where ID= '"+delete.getId()+"';";

    try {
        statement = connection.createStatement();
        statement.executeUpdate(firstQuery);
        System.out.println("Expense is deleting");

    } catch (SQLException e) {
        System.out.println("Expense isn't deleting - SQLException");
        e.printStackTrace();
    }
}

delete.jsp如下:

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Add pages</title>
    </head>
    <body>
        <form action="adminController">
            <p>
                <input type="hidden" name="operation" value="deletepage" />
                <input type="hidden" name="parentid" value="<%= request.getParameter("id") %>" />
                Enter ID of page you want to delete:<input name="id"><br>

                <input type="submit" />
            </p>
        </form>
    </body>
</html>

mainadmin.jsp: _

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Insert title here</title>
    </head>
    <body>
        <%= request.getAttribute("menu") %>
    </body>
</html>

如果您运行此代码,您会看到我的删除按钮位于每个页面名称之后。我想要做的是通过按下页面附近的删除按钮来删除这个页面,应该有类似“你确定要删除吗?”,提交按钮“删除”。但我设法做的是这样的(如在我的代码中):每当按下删除按钮时,您必须输入要删除的页面的 id,所以无论我按下什么删除按钮,它都会请求 id。但我只想立即删除它。谁能帮忙?

4

1 回答 1

2

在 adminController.java 你有

else if (operation.equals("delete")) {
    RequestDispatcher dispatcher = request.getRequestDispatcher("/delete.jsp");
    dispatcher.forward(request, response);

} else if (operation.equals("deletepage")) {
    PageData pageData = new PageData();
    pageData.setId(request.getParameter("id"));
    dao.deletePage(pageData);
    response.sendRedirect("adminController");
}

将其更改为

else if (operation.equals("delete")) {
    PageData pageData = new PageData();
    pageData.setId(request.getParameter("id"));
    dao.deletePage(pageData);
    response.sendRedirect("adminController");
}

你不需要第二个操作'deletepage',所以你不需要delete.jsp

于 2012-05-23T21:54:38.017 回答