有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

即使在成功执行删除查询之后,java更新的列表也不会显示在jsp页面中

我的jsp页面代码如下:

<form id="target" >
        <table id="table">
            <tr id="firstrow"><th>Product Name</th><th>Quantity</th><th>Price/unit</th><th>Vendor's Name</th><th>actions</th></tr>

                <c:forEach var="current" items="${sessionScope.productname}" >
                <tr id="select_one">
                    <input id="productId" type="hidden" name="productId" value="<c:out value="${current.productId}" />"/>
                    <td><input id="productName" class="box" type="" name="productName"  value="<c:out value="${current.productName}" />" readonly></td>
                    <td><input id="quantity"  class="box" type="" name="quantity"  value="<c:out value="${current.quantity}"/>" readonly></td>
                    <td><input id="price" class="box" type="" name="price"  value="<c:out value="${current.price}"/>" readonly></td>
                    <td><input id="vname" class="box" type="" name="vname" value="<c:out value="${current.vname}"/>" readonly></td>
                    <td>
                    <input class="Edit" type="button" name="action" value="Edit">
                    <input  class="delete"type="button" name="action" value="Delete">
                    </td>
            </tr>
            </c:forEach>

        </table>
    </form>

我为“Delete”按钮编写的jQuery是(这也可以正常工作)

$(document).ready(function() {

    $(".delete").click(function() 
            {
             $("#table").remove();  
                var productId=$(this).closest('tr').find("#productId").val();

                 var param = {productId:productId};

                $.ajax({
                  url: './deleteproduct',
                   data: param, 
                   type: 'post',
                    success: function(result) {
                    location.reload();
                  }
                });
            });

});

我的deleteproduct()功能代码是

public static boolean deleteProduct(int productId)  {
    boolean status=false;
    String driverClass="com.mysql.jdbc.Driver";
    String dbUrl="jdbc:mysql://localhost:3306/pmsdb?&relaxAutoCommit=true";
    String dbUser="root";
    String dbPswd="root";


    PreparedStatement pstmt=null;
    Connection con=null;

    try{

        Class.forName(driverClass);
        con=DriverManager.getConnection(dbUrl,dbUser,dbPswd);

        pstmt=con.prepareStatement("DELETE FROM `pms_prd_details` WHERE `producid`=?");
        pstmt.setInt(1, productId);
        pstmt.executeUpdate();
        con.commit();
        System.out.println("Record is deleted!");

    }catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block

        e.printStackTrace();
    } catch (NumberFormatException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 
    finally{
        if (con != null) {  
            try {  
                con.close();  
            } catch (SQLException e) {  
                e.printStackTrace();  
            }  
        }  
        if (pstmt != null) {  
            try {  
                pstmt.close();  
            } catch (SQLException e) {  
                e.printStackTrace();  
            }  
        }  

    }
    return status;
}

这也是我的servlet的doPost()方法

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    doGet(request, response);
    int productId=Integer.parseInt(request.getParameter("productId"));
    System.out.println(productId);
    DeleteProductFromList dpd=new DeleteProductFromList();
     if (dpd.deleteProduct(productId)){
            System.out.println("true");
            ShowProduclist lst= new ShowProduclist();
            request.getSession().setAttribute("productname", lst.showProductlist());
            RequestDispatcher rd=request.getRequestDispatcher("ProductList.jsp");    
            rd.include(request,response); 

     }
}

我最大的问题是,当我打电话给

request.getSession().setAttribute("productname", lst.showProductlist()); 

用于创建新记录或更新新记录,并尝试在同一jsp页面中显示,它工作正常。但在删除时,我做了同样的事情,但尽管它在我的jsp页面中成功地从数据库中删除了记录,但它显示的是已经删除的记录。发生了什么事?请帮帮我谢谢你


共 (1) 个答案

  1. # 1 楼答案

    您可能需要了解Ajax是如何工作的。使用ajax在jQuery中调用delete时,不会重新加载页面。你的客户仍然有相同的HTML,但他只是在后台发送删除请求

    所以整个通信都在JavaScript中。您的服务器可以以某种状态响应(例如200 OK),您可以在成功函数(从HTML中删除对象)中使用JavaScript/jQuery对服务器的响应执行JavaScript操作,如下所示:

    $("#item10").remove()
    

    您可以尝试使用Firebug(Firefox插件)等工具来跟踪HTML/Ajax请求,以了解发生了什么

    更新

    $(document).ready(function() {
    
        $(".delete").click(function() 
                {
    
                    var productId=$(this).closest('tr').find("#productId").val();
    
                     var param = {productId:productId};
    
                    $.ajax({
                      url: './deleteproduct',
                       data: param, 
                       type: 'post',
                        success: function(result) {
                        //find row to delete and use remove();
                      }
                    });
                });
    
    });