java - 用户删除该表中的某些信息后 JSP 表消失

标签 java mysql database jsp

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

<!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>

最佳答案

当用户单击删除链接时,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>

P.S:我认为这是一个遗留的 JSP,并且您别无选择,只能继续使用如此丑陋的代码。如果您有选择,我强烈建议您放弃这些 scriptlet 和这种全 View 架构,并开始使用 JSTL 标记、servlet 和 beans。

关于java - 用户删除该表中的某些信息后 JSP 表消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14223766/

相关文章:

java - Maven发布插件和scm插件配置

java - 如何检索 servlet 中的子域? spring有没有 helper

Ubuntu 12.04.3 x32 的 nginx VPS 上的 phpmyadmin 错误

mysql - MS-SQL 是否支持内存表?

SQL:使用存储在 table_1 中的值根据 table_2 中列出的变量填充 table_2 中的列

mysql - 如何显示 SCHEMA 中所有表的列标题

java - 使用 toString (java) 打印存储在不同数组中的对象内的对象

java - ANDROID:应用程序在插入数据时没有响应

Mysql加入rails

python - 为 url 目的生成看似随机的唯一数字