显示是/否确认对话框以操纵数据库中的数据

时间:2011-12-17 15:29:27

标签: java mysql jsp

我想从我的系统中删除用户,但它只是在成功或失败消息上显示带有“确定”按钮的警告消息。我有2个相关的.java文件,而User.java(声明用户ID)和UserManager.java(用java连接mysql数据库和操作数据,如方法deleteAUserFromDB(),你也可以在下面的jsp文件中找到)我还有2个与该java文件相关的jsp文件,而表单页面和动作文件。以下是我在我的操作文件中应用的代码:

deleteUserAction.jsp:

<%@ page import="java.lang.*" %>
<jsp:useBean class="spkp.UserManager" id="userManager" />
<jsp:useBean class="spkp.User" id="userToDelete" />
<jsp:setProperty name="userToDelete" property="userID"/>

<%boolean success=userManager.deleteAUserFromDB(userToDelete);
if(success){%>
  <script>
    alert('Success to delete!!!');
    window.location='deleteUser.jsp';
  </script>
<%}else{%>
  <script>
    alert('Fail to delete!!!');
    window.location='deleteUser.jsp';
  </script>
<%}%>

之前,deleteUser.jsp只是一个表单页面,其中显示了userID列表(使用数组),显示的每个信息的“删除”按钮以及下面是将信息发送到deleteUserAction.jsp的代码:

deleteUser.jsp

 <jsp:useBean class="spkp.UserManager" id="userManager" />
<%@page import="spkp.User"%>
<%@ page language="java" import="java.lang.*" import="java.sql.*" %>
<jsp:useBean class= "spkp.MySQLConnection" id= "view" />
<html>
<head>
</head>
<body>
  <table width="80%" align="center" border="1" bordercolor="#000000">
        <tr>
            <td width="200" height="28">UserID</td>
            <td width="100">Delete User</td>
        </tr>
    <%
    User[] allUsers = userManager.getAllUsersFromDB();
    if(allUsers!=null)
        {
    for(int i=1;i<allUsers.length;i++)
        {
        %>
        <tr>
            <td><%= allUsers[i].getUserID()%></td>
            <td align="center"><input type="button" value="Delete" onclick="window.location='deleteUserAction.jsp?userID=<%= allUsers[i].getUserID()%>'"/>
            </td>
        </tr>
        <% } } %>
     </table>
   </body>
   </html>

我找到了许多可以在JSP中应用的确认对话框的Javascript代码,例如: link1link2

但他们只是显示没有功能的框,无论我们是否单击是/否,但不是删除我选择的数据库中的任何用户。我会在几天内尽力寻求解决方案,但我真的被卡住了。

1 个答案:

答案 0 :(得分:1)

<td><%= allUsers[i].getUserID()%></td>
<td align="center"><input type="button" value="Delete" onclick="comfirm_decision('<%= allUsers[i].getUserID()%>');"/>
脚本标记

之间的

function confirm_decision(user_id){
    if(confirm("you want to delete the user?")) // this will pop up confirmation box and if yes is clicked it call servlet else return to page
     {
       window.location="deleteStaffAction.jsp?userID="+user_id; 
     }else{
       return false;
    }
   return true;
 }

file1 enter image description here enter image description here confirm window