后台SSH在做Session删除的时候,遇到了ConcurrentModificationException异常。
参考资料:http://blog.csdn.net/idesvo/article/details/26558073
问题代码:
for (BorrowEquip borrowEquip : basketList) { if(borrowEquip.getEquipId() == idTest){ basketList.remove(borrowEquip); } }
更改后代码:
for (BorrowEquip borrowEquip : basketList) { Iterator<BorrowEquip> iterator = basketList.iterator(); if(borrowEquip.getEquipId() == idTest){ iterator.next(); iterator.remove(); break; } }
删除方法的整个代码:
public String deleteBasketEquip() { Long idTest = 1L; Map<String, Object> basketMap = ActionContext.getContext().getSession(); List<BorrowEquip> basketList = new ArrayList<BorrowEquip>(); basketList = (List<BorrowEquip>)basketMap.get("Basket"); for (BorrowEquip borrowEquip : basketList) {
Iterator<BorrowEquip> iterator = basketList.iterator(); if(borrowEquip.getEquipId() == idTest){ iterator.next(); iterator.remove(); break; } } return "success"; }