@GetMapping("/shop-cart") public String cartListPage(HttpServletRequest request, HttpSession httpSession) { NewBeeMallUserVO user = (NewBeeMallUserVO) httpSession.getAttribute(Constants.MALL_USER_SESSION_KEY); int itemsTotal = 0; int priceTotal = 0; List<NewBeeMallShoppingCartItemVO> myShoppingCartItems = newBeeMallShoppingCartService.getMyShoppingCartItems(user.getUserId()); if (!CollectionUtils.isEmpty(myShoppingCartItems)) { //购物项总数 itemsTotal = myShoppingCartItems.stream().mapToInt(NewBeeMallShoppingCartItemVO::getGoodsCount).sum(); if (itemsTotal < 1) { NewBeeMallException.fail("购物项不能为空"); } //总价 for (NewBeeMallShoppingCartItemVO newBeeMallShoppingCartItemVO : myShoppingCartItems) { priceTotal += newBeeMallShoppingCartItemVO.getGoodsCount() * newBeeMallShoppingCartItemVO.getSellingPrice(); } if (priceTotal < 1) { NewBeeMallException.fail("购物项价格异常"); } } request.setAttribute("itemsTotal", itemsTotal); request.setAttribute("priceTotal", priceTotal); request.setAttribute("myShoppingCartItems", myShoppingCartItems); return "mall/cart"; }
时间: 2023-06-24 10:05:55 浏览: 39
这段代码是一个后端控制器方法,使用了Spring MVC框架的@GetMapping注解,表示处理GET请求,并且映射了路径"/shop-cart"。
该方法接受了HttpServletRequest和HttpSession两个参数,分别表示当前请求和当前会话。
在方法中,首先从会话中获取当前用户信息,然后根据用户ID获取该用户的购物车项列表。如果购物车项列表不为空,则统计购物项总数和总价,并将它们分别设置到请求对象的属性中;最后将购物车项列表也设置到请求对象的属性中。
这段代码的作用是返回购物车页面,并且展示当前用户的购物车项列表、购物项总数和总价。
相关问题
帮我改写以下代码,不要用servlet.代码为:@Controller public class CartController { @Autowired private UserService userService; @Autowired private CommodityService commodityService; @Autowired private CartService cartService; @GetMapping("/cart") public String cart(Model model, HttpServletRequest request) { HttpSession session = request.getSession(); User user = (User)session.getAttribute("user"); if (user == null) { return "redirect:/login"; } List<Cart> cartList = cartService.findByUid(user.getUid()); model.addAttribute("cartList", cartList); return "cart"; } @PostMapping("/cart/add") public String addCart(Integer cid, Integer cnum, HttpServletRequest request) { HttpSession session = request.getSession(); User user = (User)session.getAttribute("user"); if (user == null) { return "redirect:/login"; } Commodity commodity = commodityService.findById(cid); if (commodity == null) { return "redirect:/commodity"; } Cart cart = new Cart(); cart.setUid(user.getUid()); cart.setCid(cid); cart.setCnum(cnum); cartService.save(cart); return "redirect:/cart"; } @PostMapping("/cart/delete") public String deleteCart(Integer cartid) { cartService.deleteById(cartid); return "redirect:/cart"; }
以下是改写后的代码,使用了Spring Boot的@RestController注解和@ResponseBody注解来实现API风格的接口:
@RestController
public class CartController {
@Autowired
private UserService userService;
@Autowired
private CommodityService commodityService;
@Autowired
private CartService cartService;
@GetMapping("/cart")
public List<Cart> getCart(HttpServletRequest request) {
HttpSession session = request.getSession();
User user = (User)session.getAttribute("user");
if (user == null) {
return null;
}
List<Cart> cartList = cartService.findByUid(user.getUid());
return cartList;
}
@PostMapping("/cart/add")
public String addCart(Integer cid, Integer cnum, HttpServletRequest request) {
HttpSession session = request.getSession();
User user = (User)session.getAttribute("user");
if (user == null) {
return "redirect:/login";
}
Commodity commodity = commodityService.findById(cid);
if (commodity == null) {
return "redirect:/commodity";
}
Cart cart = new Cart();
cart.setUid(user.getUid());
cart.setCid(cid);
cart.setCnum(cnum);
cartService.save(cart);
return "success";
}
@PostMapping("/cart/delete")
public String deleteCart(Integer cartid) {
cartService.deleteById(cartid);
return "success";
}
}
这里将原来的@Controller注解改为@RestController注解,表示这是一个API控制器,不需要返回视图,而是直接返回数据。同时,将原来的Model和视图名称改为了List<Cart>,表示返回一个购物车列表。为了让返回的数据能够被前端页面接收到,我们使用@ResponseBody注解将返回值转为JSON格式。最后,将原来的重定向操作改为返回字符串"success",表示操作成功。
这断代码能用getmapping吗@GetMapping("/allAppliance") //接口路径 public String allAppliance(HttpServletRequest request, HttpServletResponse response,HttpSession session) { //查询全部二手家电信息方法 Buyer buyer= (Buyer) session.getAttribute("buyer"); if (buyer== null) {//如果买家没有登录 request.setAttribute("error","请先登陆后再浏览家电");return "login";} List<Appliance>appliances= applianceService.selectallApp(); //调用applianceService中的selectallApp查询全部二手家电信息 request.setAttribute("appliances",appliances); return "applianceby";} //查询成功,返回二手家电信息
是的,这段代码可以使用 @GetMapping 注解,因为这是一个用于查询全部二手家电信息的方法,并且不涉及对数据进行修改操作。以下是使用 @GetMapping 注解的示例:
```java
@GetMapping("/allAppliance")
public String allAppliance(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
Buyer buyer = (Buyer) session.getAttribute("buyer");
if (buyer == null) {
request.setAttribute("error", "请先登录后再浏览家电");
return "login";
}
List<Appliance> appliances = applianceService.selectAllApp();
request.setAttribute("appliances", appliances);
return "applianceby";
}
```
在上述示例中:
- 使用 @GetMapping 注解将方法映射到 "/allAppliance" 路径。
- 在方法中,首先获取买家对象,如果买家对象为 null,即买家没有登录,则设置一个错误提示信息并返回 "login" 页面。
- 接着调用 applianceService 的 selectAllApp 方法来查询全部二手家电信息,并将结果存放在名为 "appliances" 的 request 属性中。
- 最后,返回 "applianceby" 页面,用于展示查询到的二手家电信息。
请确保在实际使用时,进行单元测试和异常处理,以确保代码在各种情况下都能正确运行。