【问题标题】:Don't know how to iterate over supplied "items" in forEach ArrayList of objects(shopping cart) [duplicate]不知道如何迭代对象(购物车)的 forEach ArrayList 中提供的“项目”[重复]
【发布时间】:2019-10-02 10:45:29
【问题描述】:

好的,所以我试图通过在 servlet 的 ArrayList 中引入产品来将产品添加到购物车,但 jstl“forEach”不知道如何迭代它,所以我猜我做得不好(我jsp-servlet 新手)

我可以添加产品,但以前的产品被覆盖,所以现在我尝试将它们存储到一个 ArrayList 中,我存储在一个会话变量中,当我需要添加另一个产品时,我可以从该变量访问它。(我实际上并没有知道这是否是一个好方法,但对我来说似乎没问题)。这也是我发布的第一个问题,如果我做得不好,我很抱歉。

@WebServlet(name = "mycart")
public class mycart extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession(true);
        Product product = new Product();
        ShoppingCart items = new ShoppingCart();
        items.copyItems((ArrayList<Product>) session.getAttribute("sessionProducts"));
        session.removeAttribute("sessionProducts");
        String code = request.getParameter("code");

        if(items.equals(null)){
            items.addProduct(product.productById(code));
            request.setAttribute("listofproducts", items.getCart());
            session.setAttribute("sessionProducts", items.getCart());
            request.getRequestDispatcher("/cart.jsp").forward(request, response);
        }
        else{
            session.setAttribute("sessionProducts", product.productById(code));
            request.setAttribute("listofproducts", product.productById(code));
            request.getRequestDispatcher("/cart.jsp").forward(request, response);
        }


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
<table border="1px solid">
            <tr>
                <th>Name</th>
                <th>Quantity</th>
                <th>Code</th>
                <th>Price</th>
            </tr>

            <tr>
                <c:forEach items="${listofproducts}" var="list">
                    <tr>
                        <td>${list.name}</td>
                        <td>${list.quantity}</td>
                        <td>${list.code}</td>
                        <td>${list.price}</td>
                    </tr>
                </c:forEach>
            </tr>

        </table>
public class ShoppingCart {

    public ArrayList<Product> items = new ArrayList<>();

    public void addProduct(Product product){
        this.items.add(product);
    }

    public ArrayList<Product> getCart(){
        return this.items;
    }

    public void copyItems(ArrayList<Product> items){
        this.items = items;
    }

}

所有产品都有一个“添加到购物车”按钮,所以我只想在单击该按钮时将每个产品添加到购物车。

【问题讨论】:

  • 为什么你绝对肯定行 request.setAttribute("listofproducts", items.getCart()); 被执行而不是 request.setAttribute("listofproducts", product.productById(code));
  • 其实我尝试转发一些随机请求参数。似乎当第一个产品添加到列表中时,forEach 语句无法真正迭代请求变量。如果我不使用 forEach 就可以了。像 ${listofproducts.name} ${listofproducts.quantity} ${listofproducts.code} ${listofproducts。价格}
  • 我不是那个意思.. request.setAttribute("listofproducts", product.productById(code)); 行显然没有设置产品的列表,而是单个产品。你将如何迭代它?
  • 是的,我是新手。我想我只存储一个产品,但我真的不知道如何以正确的方式去做。我发布了一个答案。你认为这是一个好的方法吗?

标签: java jsp servlets jakarta-ee jstl


【解决方案1】:

我这样更新了我的 servlet:

public class mycart extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        HttpSession session = request.getSession(false);
        Product product=new Product();
        String code = request.getParameter("code");
        List<Product> list = (List<Product>) session.getAttribute("sessionProducts");
        if(code != null) {
            if (list == null) {
                list = new ArrayList<>();
            }
            list.add(product.productById(code));
            session.removeAttribute("sessionProducts");
            session.setAttribute("sessionProducts", list);
            request.setAttribute("listofproducts", list);
            request.getRequestDispatcher("/cart.jsp").forward(request, response);
        }
        else{
            request.setAttribute("listofproducts", list);
            request.getRequestDispatcher("/cart.jsp").forward(request, response);
        }


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}

现在可以了。其他部分适用于您只想检查之前添加到购物车中的产品的情况。我会做更多的更改以添加一些其他功能,但我想如果有人搜索这个问题,我希望它对你有所帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-26
    • 2013-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多