0

在这段代码中,当用户添加删除按钮时,raw 正在从数据库中删除,但用户被重定向到空主页,除非他注销并再次登录。我该如何修复代码,以便当他删除某些内容时,整个布局不会消失,而只会消失他删除的行。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>




<script type="text/javascript">

function del() {
    if (confirm("Do You Want to Delete this Menu?")) {
    } else {
        return false;
    }
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<link rel="stylesheet" href="images/style.css" type="text/css"
charset="utf-8" />
</head>
<body>
<%

    menu_slno1 = request.getParameter("menu_slno");
    if (menu_slno1 != null)
        menu_slno = Integer.parseInt(menu_slno1);
    delete_menu = request.getParameter("delete_menu");


    if ("yes".equals(delete_menu)) {
        MenuId = request.getParameter("MenuId");
        x = stmt1
                .executeUpdate("Delete from menu where MenuId="
                        + MenuId);
    }
%>


<center><h2>VIEW MENU</h2></center>

<center><table width="736" height="97" border="1"></center>
    <%
        if (x == 1) {
    %>
    <tr bgcolor="gray">
        <th height="35" colspan="9"><div align="center">
                Menu deleted successfully!
            </div></th>
    </tr>
    <%
        }
    %>
    <tr bgcolor="gray">
        <td><div align="center">
                <strong>Menu ID</strong>
            </div></td>
        <td><div align="center">
                <strong>Name </strong>
            </div></td>
        <td><div align="center">
                <strong>Info</strong>
            </div></td>
        <td><div align="center">
                <strong>Price</strong>
            </div></td>
        <td colspan="2"><div align="center">
                <strong>Action</strong>
            </div></td>
    </tr>
    <%

     String sUserID=request.getParameter("username");
      session.setAttribute("username", sUserID);
        int icount = 0;
        rs = stmt.executeQuery("SELECT menu.menuID, menu.name, menu.info, menu.price, menu.RestaurantID  FROM menu INNER JOIN clients ON menu.username = clients.username where menu.username='" +sUserID+ "'");

        while (rs.next()) {
            //menu_slno = rs.getInt("menu_slno");
            MenuId = rs.getString("MenuId");
        %>
    <tr>
        <td><div align="center"><%=++icount%></div></td>

        <td><%=rs.getString("Name")%></td>
        <td><%=rs.getString("Info")%></td>
        <td><%=rs.getDouble("Price")%></td>

        <td><div align="center">
                <a href="edit_menu.jsp?MenuId=<%=MenuId%>">Edit</a>
            </div></td>
        <td><div align="center">
                <a
                    href="view_menu.jsp?delete_menu=yes&MenuId=    <%=MenuId%>&MenuId=<%=MenuId%>"
                    onclick="return del()">Delete</a>
            </div></td>
    </tr>
    <%
        }
    %>
</table>
<a href="add_menu.jsp">Add Menu</a>

</body>
</html>
4

1 回答 1

1

当用户点击删除链接时,该username参数未在 URL 中设置。因此,当页面刷新时,String sUserID=request.getParameter("username");调用返回null或为空,然后页面的其余部分不显示,因为以下 SQL 查询没有返回有效用户名。

似乎您只需要在&username=<%=sUserID%>删除链接中添加类似内容即可使其正常工作:

<a href="view_menu.jsp?delete_menu=yes&MenuId=<%=MenuId%>&username=<%=sUserID%>" onclick="return del()">Delete</a>

PS:我假设这是一个遗留的 JSP,你别无选择,只能继续使用这些丑陋的代码。如果您有选择,我强烈建议您放弃那些 scriptlet 和这种全视图架构,并开始使用 JSTL 标记、servlet 和 bean。

于 2013-01-09T01:35:26.817 回答