Eclipse实现Java Web购物车功能攻略
1. 创建Maven Web Project
首先,在Eclipse中创建一个Maven Web Project。在创建时,需要选择以下选项:
- 勾选“Create a simple project(创建简单项目)”
- 选择“war”项目打包方式
在创建好的项目中,需要在pom.xml文件中添加以下依赖:
<!-- Spring MVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<!-- JSTL -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- Servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<!-- JSP -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- Spring ORM + Hibernate ORM -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.18.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.4.18.Final</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.21</version>
</dependency>
<!-- MySQL Connector -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.17</version>
</dependency>
2. 创建数据库表
在MySQL中创建两个表:goods和cart。其中,goods表中存储商品信息,cart表中存储用户购物车信息。
3. 创建实体类
在Java源代码目录下,创建Goods和Cart实体类,分别对应goods表和cart表。
4. 创建商品展示页面
在WebContent目录下,创建showGoods.jsp页面。该页面展示数据库中的商品列表,并提供添加到购物车的按钮。
示例代码:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<meta charset="UTF-8">
<title>商品展示</title>
</head>
<body>
<table>
<thead>
<tr>
<th>商品名称</th>
<th>价格</th>
<th>库存</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<c:forEach items="${goodsList}" var="goods">
<tr>
<td>${goods.name}</td>
<td>${goods.price}</td>
<td>${goods.stock}</td>
<td><a href="${pageContext.request.contextPath}/addCart?id=${goods.id}">添加到购物车</a></td>
</tr>
</c:forEach>
</tbody>
</table>
</body>
</html>
5. 创建添加购物车功能
在CartController中,创建处理添加购物车请求的方法。该方法将用户选中的商品加入到购物车。当用户添加商品到购物车时,会调用该方法。
示例代码:
@RequestMapping(value = "/addCart", method = RequestMethod.GET)
public ModelAndView addCart(HttpServletRequest request) {
int goodsId = Integer.parseInt(request.getParameter("id"));
ModelAndView mav = new ModelAndView("showGoods");
HttpSession session = request.getSession();
List<Cart> cartList = (List<Cart>) session.getAttribute("cart");
if (cartList == null) {
cartList = new ArrayList<Cart>();
session.setAttribute("cart", cartList);
}
boolean exist = false;
for (Cart cart : cartList) {
if (cart.getGoods().getId() == goodsId) {
cart.setCount(cart.getCount() + 1);
exist = true;
break;
}
}
if (!exist) {
Goods goods = goodsService.getGoods(goodsId);
Cart cart = new Cart();
cart.setGoods(goods);
cart.setCount(1);
cartList.add(cart);
}
mav.addObject("goodsList", goodsService.getGoodsList());
return mav;
}
6. 创建购物车展示页面
在WebContent目录下,创建showCart.jsp页面。该页面展示用户购物车中已经添加的商品,并提供结算功能。
示例代码:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<meta charset="UTF-8">
<title>购物车</title>
</head>
<body>
<table>
<thead>
<tr>
<th>商品名称</th>
<th>价格</th>
<th>数量</th>
<th>小计</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<c:if test="${not empty cartList}">
<c:set var="total" value="0"/>
<c:forEach items="${cartList}" var="cart">
<tr>
<td>${cart.goods.name}</td>
<td>${cart.goods.price}</td>
<td>${cart.count}</td>
<td>${cart.goods.price * cart.count}</td>
<td><a href="${pageContext.request.contextPath}/removeCart?id=${cart.goods.id}">删除</a></td>
</tr>
<c:set var="total" value="${total + cart.goods.price * cart.count}"/>
</c:forEach>
<tr>
<td colspan="3">总计:</td>
<td>${total}</td>
<td><a href="${pageContext.request.contextPath}/submitOrder">结算</a></td>
</tr>
</c:if>
</tbody>
</table>
</body>
</html>
7. 创建删除购物车中的商品功能
在CartController中,创建处理删除购物车中商品请求的方法。该方法将用户选中的商品从购物车中删除。
示例代码:
@RequestMapping(value = "/removeCart", method = RequestMethod.GET)
public ModelAndView removeCart(HttpServletRequest request) {
int goodsId = Integer.parseInt(request.getParameter("id"));
HttpSession session = request.getSession();
List<Cart> cartList = (List<Cart>) session.getAttribute("cart");
for (Cart cart : cartList) {
if (cart.getGoods().getId() == goodsId) {
cartList.remove(cart);
break;
}
}
ModelAndView mav = new ModelAndView("showCart");
mav.addObject("cartList", cartList);
return mav;
}
8. 创建结算功能
在CartController中,创建处理结算请求的方法。该方法会将用户购买的商品信息存入数据库(订单表和订单明细表),并将用户购物车清空。
示例代码:
@RequestMapping(value = "/submitOrder", method = RequestMethod.GET)
public ModelAndView submitOrder(HttpServletRequest request) {
HttpSession session = request.getSession();
List<Cart> cartList = (List<Cart>) session.getAttribute("cart");
Order order = new Order();
order.setOrderTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
order.setTotalPrice(0);
order.setUser(/* 用户信息 */);
orderService.addOrder(order);
for (Cart cart : cartList) {
OrderDetail detail = new OrderDetail();
detail.setOrder(order);
detail.setGoods(cart.getGoods());
detail.setCount(cart.getCount());
detail.setUnitPrice(cart.getGoods().getPrice());
detailService.addOrderDetail(detail);
order.setTotalPrice(order.getTotalPrice() + detail.getCount() * detail.getUnitPrice());
cartList.clear();
session.setAttribute("cart", cartList);
}
ModelAndView mav = new ModelAndView("showOrder");
mav.addObject("order", order);
return mav;
}
9. 创建查看订单功能
在OrderController中,创建处理查看订单请求的方法。该方法会展示用户已经完成的订单信息。
示例代码:
@RequestMapping(value = "/showOrder", method = RequestMethod.GET)
public ModelAndView showOrder(HttpServletRequest request) {
int orderId = Integer.parseInt(request.getParameter("id"));
Order order = orderService.getOrder(orderId);
ModelAndView mav = new ModelAndView("showOrder");
mav.addObject("order", order);
return mav;
}
10. 打包和部署项目
最后,将该Java Web项目打包war包,并部署到Tomcat服务器上,即可完成Java Web购物车功能的实现。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:eclipse的web项目实现Javaweb购物车的方法 - Python技术站