不知道如何遍历对象的forEach ArrayList(购物车)中提供的“项目” - java

This question already has answers here:

javax.servlet.ServletException: javax.servlet.jsp.JspTagException: Don't know how to iterate over supplied “items” in <forEach>

(3个答案)

去年关闭。

好的,所以我试图通过在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;
    }

}

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

java参考方案

我这样更新了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);
    }
}

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

当回复有时是一个对象有时是一个数组时,如何在使用改造时解析JSON回复? - java

我正在使用Retrofit来获取JSON答复。这是我实施的一部分-@GET("/api/report/list") Observable<Bills> listBill(@Query("employee_id") String employeeID); 而条例草案类是-public static class…

java.net.URI.create异常 - java

java.net.URI.create("http://adserver.adtech.de/adlink|3.0") 抛出java.net.URISyntaxException: Illegal character in path at index 32: http://adserver.adtech.de/adlink|3.0 虽然n…

java:继承 - java

有哪些替代继承的方法? java大神给出的解决方案 有效的Java:偏重于继承而不是继承。 (这实际上也来自“四人帮”)。他提出的理由是,如果扩展类未明确设计为继承,则继承会引起很多不正常的副作用。例如,对super.someMethod()的任何调用都可以引导您通过未知代码的意外路径。取而代之的是,持有对本来应该扩展的类的引用,然后委托给它。这是与Eric…

Java-如何将此字符串转换为日期? - java

我从服务器收到此消息,我不明白T和Z的含义,2012-08-24T09:59:59Z将此字符串转换为Date对象的正确SimpleDateFormat模式是什么? java大神给出的解决方案 这是ISO 8601标准。您可以使用SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM…

在Map中,如果我们使用现有键进行修改,则不会获得ConcurrentModificationException - java

我有以下代码,我希望从情况2的情况下抛出ConcurrentModificationException,但它运行成功。据我所知,如果我对地图中的单个键执行相同的操作,则不会抛出异常,因为here但是当我重现这种具有两个案例的多个密钥的场景时,通过新密钥修改。通过现有密钥进行修改。情况1: Map<String,String> mp = new H…