我有一个调用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。但我只想立即删除它。 有人可以帮忙吗?
答案 0 :(得分:2)
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