如何获取列表以从JSP中删除对象

时间:2018-09-20 12:06:47

标签: java html jsp servlets

这是我制作的JSP,它可以连接到我的控制器

<html>
        <%List<Product> products = (List<Product>) session.getAttribute("customerProductsList");
        List<Product> customerProducts = (List<Product>) session.getAttribute("customerProductsList2");
        %>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Checkout Cart</title>
    </head>



<body action="customerProductList" method="post">
<h1>Products in your Cart</h1>
    <h2>List Of Products</h2>


    <table style="background-color: buttonshadow; background-color: fuchsia;background-position: center; background:activeborder;font: sans-serif;border-style: groove;align-items: center;">
        <tr>

        </tr>
        <%if (products == null||products.size()==0) {

            out.println("This Basket is Empty.");
        }
        else { double sum =  0.0d;
                for (int i = 0;  i < products.size(); i++){
                    Double subtotal = products.get(i).getPrice();
                    sum = sum + subtotal;

                    out.println("<tr>");
                    out.println("<td>");
                    out.println(products.get(i).getName());
                    out.println("</td>");
                    out.println("<td>");
                    out.println(products.get(i).getType());
                    out.println("</td>");
                    out.println("<td>");
                    out.println(products.get(i).getBrand());
                    out.println("</td>");
                    out.println("<td>");
                    out.println(products.get(i).getPrice());
                    out.println("</td>");
                    out.println("<td>");
                    out.println("<a href='DeleteFromCartController?i=" + i + "'> Delete From Cart </a>");
                    out.println("</td>");
                    out.println("</tr>");

这是我制作的控制器,其目的是删除列表中的一项,但是每次我单击删除按钮时,它都不会从我制作的列表中删除该项目有人没有任何解决方法对我来说?

public class DeleteFromCartController extends HttpServlet {
    private static final long serialVersionUID = 1L;

    private ProductService productService;
    private HttpSession httpSession;

    public DeleteFromCartController() {
        this.productService = new ProductServiceImplementation();
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession httpSession = request.getSession(false);

        String id = request.getParameter("i");

        System.out.println(id);

        Integer idd = Integer.parseInt(id);

        System.out.println(idd);

        List<Product> customerProducts = (List<Product>) httpSession.getAttribute("customerProductsList");

        System.out.println(customerProducts);

        List<Product> customerProducts2 = customerProducts;

        customerProducts2.remove(idd);

        System.out.println(customerProducts2);

        this.httpSession = request.getSession();

        this.httpSession.setAttribute("customerProductsList2", customerProducts2);

        response.sendRedirect("Cart.jsp");

    }

0 个答案:

没有答案