好吧,所以我试图通过在 servlet 中的 ArrayList 中引入产品来将产品添加到购物车,但是 jSTL“forEach”不知道如何迭代它,所以我猜我做得不好(我是jsp-servlet 新手)
我可以添加产品,但以前的产品被覆盖,所以现在我尝试将它们存储到 ArrayList 中,我将其存储在 session 变量中,当我需要添加另一个产品时,我可以从该变量访问它。(我实际上并不这样做)知道这是否是一个好方法,但对我来说似乎没问题)。这也是我发布的第一个问题,所以如果我做得不好,我很抱歉。
@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;
}
}
所有产品都有一个“添加到购物车”按钮,所以我只想在单击该按钮时将每个产品添加到购物车。
最佳答案
我像这样更新了我的 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 部分适用于您只想检查之前添加到购物车的产品的情况。我会做一些更多的改变来添加一些其他功能,但我想如果有人搜索这个问题,我希望它对你有帮助
关于java - 不知道如何迭代 forEach ArrayList 对象(购物车)中提供的 "items",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56146808/