这是我的java类的代码
@RequestMapping(value = "/addToCart", method = RequestMethod.POST)
public String addToCart(@ModelAttribute("cartPageFlag") ArrayList<Boolean> cartPageFlag,
@ModelAttribute("productData") ProductData productData,final Model model,HttpSession httpSession){
List<ProductData> timepass = new ArrayList<>();
List<ProductData> previousCart = new ArrayList<ProductData>();
timepass.addAll(previousCart);
if(previousCart != null)
{
Iterator<ProductData> iterator =timepass.iterator();
while(iterator.hasNext()){
ProductData data = iterator.next();
iterator.remove();
}
}
return REDIRECT_PREFIX + "/restaurantMenu" ;
}
我的问题是: 我听说如果我们想要从列表中删除特定条目,那么我们可以使用迭代器但是在尝试这样做时我会得到以下错误
HTTP Status 500 - Request processing failed; nested exception is java.lang.IllegalStateException
@RequestMapping(value = "/addToCart", method = RequestMethod.POST)
public String addToCart(@ModelAttribute("cartPageFlag") ArrayList<Boolean> cartPageFlag,
@ModelAttribute("productData") ProductData productData,final Model model,HttpSession httpSession){
List<ProductData> updatedCart = new ArrayList<>();
List<ProductData> timepass = new ArrayList<>();
List<ProductData> previousCart = new ArrayList<ProductData>();
StudentData studentData =(StudentData)httpSession.getAttribute("student");
if(studentData==null){
isUserLoggedIn = false;
previousCart = (List<ProductData>) httpSession.getAttribute("sessionCart");
if (null != previousCart) {
updatedCart.addAll(previousCart);
timepass.addAll(previousCart);
}
}
else {
previousCart = listofProducts.retrieveCartEntriesFromDb(studentData.getName());
}
if(previousCart != null)
{
Iterator<ProductData> iterator =timepass.iterator();
System.out.println(iterator);
while(iterator.hasNext()){
ProductData data = iterator.next();
iterator.remove();
}
System.out.println(iterator);
}
if(previousCart !=null ){
for (ProductData cartItem : previousCart) {
if(productData.getProductName().equalsIgnoreCase(cartItem.getProductName())){
int quantityOfItem = productData.getQuantity()+cartItem.getQuantity();
productData.setQuantity(quantityOfItem);
updatedCart.remove(cartItem);
}
}
}
updatedCart.add(productData);
if(!isUserLoggedIn){
httpSession.setAttribute("sessionCart", updatedCart);
}
else {
for (ProductData cartEntryData : updatedCart) {
listofProducts.insertCartEntriesIntoDb(studentData.getName(),cartEntryData.getProductName(),cartEntryData.getQuantity());
}
}
return REDIRECT_PREFIX + "/restaurantMenu" ;
}