Order order=orderService.findOrderById(oid); List<Goods>goodsList=order.getGoodsList(); for (Goods goods:goodsList) { if(goods.getId()==gid){ System.out.println(gid+"----------------------------------------+++++++++++++++++++++++++++++++"); goodsList.remove(goods); break; } } order.setGoodsList(goodsList); orderService.save(order); System.out.println(orderService.findOrderById(oid).getGoodsList()); if(order.getGoodsList().size()==0){ orderService.deleteOrderById(oid); return Result.success(true); } order.setGoodsList(goodsList); return Result.success(true); }这段代码报不能删除
时间: 2023-06-18 13:04:06 浏览: 58
这段代码报错的原因可能是在对 `goodsList` 进行遍历删除时,使用了 `remove` 方法,这样会改变集合的大小,导致遍历出现问题。可以考虑使用迭代器或者倒序遍历来避免这个问题,例如:
```
Iterator<Goods> iterator = goodsList.iterator();
while (iterator.hasNext()) {
Goods goods = iterator.next();
if (goods.getId() == gid) {
iterator.remove();
break;
}
}
```
或者:
```
for (int i = goodsList.size() - 1; i >= 0; i--) {
Goods goods = goodsList.get(i);
if (goods.getId() == gid) {
goodsList.remove(i);
break;
}
}
```
另外,注意在删除商品后要判断 `goodsList` 是否为空,可以改为 `if (goodsList.isEmpty())`。最后,不需要再次设置 `order.setGoodsList(goodsList)`,因为已经在删除商品时修改了 `order` 的 `goodsList`。
相关问题
@RequestMapping("/completed") public String Completed(Model model,HttpSession session) { User user = (User) session.getAttribute("user"); int userid=user.getUserId(); List<OrderForm> formList = orderService.FindAllbyidandstatus(userid,4); model.addAttribute(formList); return "order4"; }我这样写传了List到A页面,那么我在A页面中,写<a>标签进行页面跳转,并且要用到List中的数据作为参数也在<a>中传递到下一个控制器,我该怎么写
如果你想在A页面中使用List中的数据作为参数,并通过<a>标签将数据传递到下一个控制器,你可以按照以下步骤进行操作:
首先,在A页面中,你可以使用Thymeleaf模板引擎来遍历List,并在<a>标签中传递数据作为参数。例如:
```html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Your A Page</title>
</head>
<body>
<h1>Your A Page</h1>
<ul>
<li th:each="form : ${formList}">
<a th:href="@{/your-next-controller-url(data=${form.id})}">Click here</a>
</li>
</ul>
</body>
</html>
```
在上面的代码中,`th:each`指令用于遍历List中的数据,`form` 是迭代变量,`${formList}` 是传递给A页面的List。
在`<a>`标签中,`th:href`属性使用Thymeleaf的URL表达式来设置链接的URL。`@{/your-next-controller-url(data=${form.id})}` 表示链接的URL为 `/your-next-controller-url`,并将`form.id`作为参数名为`data`的参数传递给下一个控制器。
然后,在下一个控制器中,你可以使用`@RequestParam`注解来接收传递的参数。例如:
```java
@Controller
public class YourNextController {
@GetMapping("/your-next-controller-url")
public String yourNextControllerMethod(@RequestParam("data") int data) {
// 在这里处理接收到的数据
// 返回相应的结果或视图
return "your-next-view";
}
}
```
在上面的代码中,`@GetMapping("/your-next-controller-url")` 注解指定了控制器方法处理的URL。`@RequestParam("data")` 注解指定了接收的参数名称。
当用户点击链接时,将会调用`yourNextControllerMethod`方法,并将传递的参数值作为参数传递给该方法。你可以在方法内部处理数据,并返回相应的结果或视图。
请注意,上述示例中的URL和参数名称仅供参考,你需要根据自己的实际情况进行调整。
public String myOrders(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException { String uid = request.getParameter("uid"); List<Order> orderList = orderService.getOrdersByUid(uid); User user = (User)request.getSession().getAttribute("user"); for (Order order : orderList) { order.setUser(user); orderService.mybatis(order); } request.getSession().setAttribute("orderList", orderList); request.setAttribute("orderList", orderList); request.setAttribute("listLen", orderList.size()); request.setAttribute("current_page", 1);
这是一个用于获取用户订单并设置相关属性的方法。它接受一个 HttpServletRequest 对象和一个 HttpServletResponse 对象作为参数,并抛出 ServletException、IOException 和 SQLException 异常。
在方法内部,通过 request.getParameter 方法获取 uid(用户 ID)的值,并将其存储在 uid 字符串变量中。
接着,调用 orderService 的 getOrdersByUid 方法,传入 uid 参数,以获取该用户的订单列表。将返回的订单列表存储在 orderList 变量中。
然后,通过 request.getSession().getAttribute 方法获取名为 "user" 的属性,并将其转换为 User 对象并存储在 user 变量中。
接下来,使用增强的 for 循环遍历 orderList 列表中的每个订单对象。对于每个订单对象,将 user 设置为订单的用户,并调用 orderService 的 mybatis 方法进行相关处理。
接着,通过 request.getSession().setAttribute 方法将 orderList 存储在会话中,以便在其他页面中访问。
然后,通过 request.setAttribute 方法将 orderList 存储在 request 对象中,以便在前端页面中使用。
最后,通过 request.setAttribute 方法设置相关属性值:listLen 表示订单列表的长度,current_page 表示当前页面的页码。
请注意,该方法可能会抛出 ServletException、IOException 和 SQLException 异常,需要进行适当的异常处理。