20-Javaweb-实战3

目录

一.案例1-将商品添加到购物车

1.1.步骤分析

1.1.1.商品详情页面,输入购买的数量,点击加入购物车

1.1.2.在cartservlet中的add方法操作

1.1.3.修改cart.jsp

二.案例2-对购物车进行操作(删除)

2.1.步骤分析

2.1.1.在购物车页面上,点击删除

2.1.2.在cartServlet中编写remove

三.案例3-对购物车进行操作(清空购物车)

3.1.步骤分析

3.1.1.在购物车页面上,有一个清空购物车的连接

3.1.2.在cartServlet中需要提供 clear

    3.1.3.判断购物车是否为空

四.案例4-生成订单

4.1.创建表

4.2.创建bean

4.3.步骤分析

4.3.1.点击生成订单

4.3.2.创建OrderServlet

4.3.3.调用orderservice 生成订单

4.4.数据库备份


 

  • 一.案例1-将商品添加到购物车

需求:
    在商品详情页面上,输入购买的数量,点击加入购物车,在购物车页面上展示里面所有的商品
分析:
    涉及的实体:
        购物车 购物车项 商品

  • 购物车中的内容

            购物车项  的map集合(有序)  (map<商品的id,购物车项>)---有删除操作用map不用list
            总金额

  •  购物车的操作:面向对象思想

            add2Cart(购物车项)---添加
            removeFromCart(String 商品的id)---删除
            clearCart() ---清空

public class Cart implements Serializable{
	//存放购物车项的集合  key:商品的id  cartitem:购物车项   使用map集合便于删除单个购物车项
	private Map<String, CartItem> map=new LinkedHashMap<>();
	
	//总金额
	private Double total=0.0;
	
	/**
	 * 获取所有的购物车项
	 * @return
	 */
	public Collection<CartItem> getItmes(){
		return  map.values();
	}
	/**
	 * 添加到购物车
	 * @param item 购物车项
	 */
	public void add2Cart(CartItem item){
		//1.先判断购物车中有无该商品
		//1.1先获取商品的id
		String pid = item.getProduct().getPid();
		if(map.containsKey(pid)){
			//有
			//设置购买数量 需要获取该商品之前的购买数量+现在的购买数量(item.getCount)
			//获取购物车中购物车项
			CartItem oItem = map.get(pid);
			oItem.setCount(oItem.getCount()+item.getCount());
		}else{
			//没有 将购物车项添加进去
			map.put(pid, item);
		}
		
		//2.添加完成之后 修改金额
		total+=item.getSubtotal();
	}
	
	/**
	 *  从购物车删除指定购物车项
	 * @param pid 商品的id
	 */
	public void removeFromCart(String pid){
		//1.从集合中删除
		CartItem item = map.remove(pid);
		
		//2.修改金额
		total-=item.getSubtotal();
	}
	
	/**
	 * 清空购物车
	 */
	public void clearCart(){
		//1.map置空
		map.clear();
		
		//2.金额归零
		total=0.0;
	}
	
	

	public Map<String, CartItem> getMap() {
		return map;
	}

	public void setMap(Map<String, CartItem> map) {
		this.map = map;
	}

	public Double getTotal() {
		return total;
	}

	public void setTotal(Double total) {
		this.total = total;
	}
	
	
}
  • 购物项中的内容

            商品对象
            购买数量
            小计

/**
 * 购物车项
 * @author Administrator
 *
 */
public class CartItem implements Serializable{
	private Product product;//商品对象
	private Integer	count;//购买数量
	private Double	subtotal=0.0;//小计
	
	public Product getProduct() {
		return product;
	}
	public void setProduct(Product product) {
		this.product = product;
	}
	public Integer getCount() {
		return count;
	}
	public void setCount(Integer count) {
		this.count = count;
	}
	public Double getSubtotal() {
		return product.getShop_price()*count;
	}
	
	public CartItem(Product product, Integer count) {
		this.product = product;
		this.count = count;
	}
	public CartItem() { }
	
	
	
}

 

  • 1.1.步骤分析

  • 1.1.1.商品详情页面,输入购买的数量,点击加入购物车

    /store/cart?method=add&pid=??&count=??

<form id="formId" action="${pageContext.request.contextPath }/cart?method=add" method="post">
	<input type="hidden" name="pid" value="${bean.pid }">
	<div><strong>${bean.pname }</strong></div>
	<div style="border-bottom: 1px dotted #dddddd;width:350px;margin:10px 0 10px 0;">
		<div>编号:${bean.pid }</div>
	</div>

	<div style="margin:10px 0 10px 0;">商城价: <strong style="color:#ef0101;">¥:${bean.shop_price }元/份</strong> 参 考 价: <del>¥${bean.market_price }元/份</del>
	</div>

	<div style="margin:10px 0 10px 0;">促销: <a target="_blank" title="限时抢购 (2014-07-30 ~ 2015-01-01)" style="background-color: #f07373;">限时抢购</a> </div>

	<div style="padding:10px;border:1px solid #e7dbb1;width:330px;margin:15px 0 10px 0;;background-color: #fffee6;">
		<div style="margin:5px 0 10px 0;">白色</div>

		<div style="border-bottom: 1px solid #faeac7;margin-top:20px;padding-left: 10px;">购买数量:
			<input id="quantity" name="count" value="1" maxlength="4" size="10" type="text"> </div>
		<div style="margin:20px 0 10px 0;;text-align: center;">
			<a href="javascript:void(0)" onclick="addCart()">
				<input style="background: url('${pageContext.request.contextPath}/images/product.gif') no-repeat scroll 0 -600px rgba(0, 0, 0, 0);height:36px;width:127px;" value="加入购物车" type="button">
			</a> &nbsp;收藏商品</div>
	</div>
</form>


<script type="text/javascript">
    function addCart(){
        //将表单提交
        document.getElementById("formId").submit();
    }
</script>

 

  • 1.1.2.在cartservlet中的add方法操作

  • 先获取两个参数 pid    和 count
  •  调用ProductService 通过id获取一个商品
  • 拼装CartItem

        Product--查询出来
        count--传递过来了
        suctotal--计算

  • 获取购物车,调用add2Cart(cartitem)
  • 页面跳转

    重定向 /jsp/cart.jsp----展示给用户的路径更清晰

添加一个获取购物车(从session中获取)的方法 

	/**
	 * 获取购物车
	 * @param request
	 * @return
	 */
	public Cart getCart(HttpServletRequest request){
		Cart cart=(Cart) request.getSession().getAttribute("cart");
		//判断购物车是否为空
		if(cart == null){
			//创建一个cart
			cart=new Cart();
			
			//添加到session中
			request.getSession().setAttribute("cart", cart);
		}
		return cart;
	}

为了方便在jsp页面中遍历集合中的product,在Cart中添加bean导航

	/**
	 * 获取所有的购物车项
	 * @return
	 */
	public Collection<CartItem> getItmes(){
		return  map.values();
	}
	/**
	 * 添加到购物车
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception 
	 */
	public String add(HttpServletRequest request, HttpServletResponse response) throws Exception {
		//1.获取pid和数量
		String pid = request.getParameter("pid");
		int count = Integer.parseInt(request.getParameter("count"));
		
		//2.调用productservice 通过pid获取一个商品
		ProductService ps=(ProductService) BeanFactory.getBean("ProductService");
		Product product = ps.getByPid(pid);
		
		//3.组装成CartItem
		CartItem cartItem = new CartItem(product, count);
		
		//4.添加到购物车
		Cart cart = getCart(request);
		cart.add2Cart(cartItem);
		
		//5.重定向
		response.sendRedirect(request.getContextPath()+"/jsp/cart.jsp");
		return null;
	}

1.1.3.修改cart.jsp

<c:forEach items="${cart.itmes }" var="item">
	<tr class="active">
		<td width="60" width="40%">
			<input type="hidden" name="id" value="22">
			<img src="${pageContext.request.contextPath}/${item.product.pimage}" width="70" height="60">
		</td>
		<td width="30%">
			<a target="_blank">${item.product.pname }</a>
		</td>
		<td width="20%">
			¥${item.product.shop_price }
		</td>
		<td width="10%">
			<input type="text" name="quantity" value="${item.count }" maxlength="4" size="10" readonly="readonly">
		</td>
		<td width="15%">
			<span class="subtotal">¥${item.subtotal }</span>
		</td>
		<td>
			<a href="javascript:void(0);" class="delete" onclick="removeFromCart('${item.product.pid}')">删除</a>
		</td>
	</tr>
</c:forEach>

 

  • 二.案例2-对购物车进行操作(删除)

  • 2.1.步骤分析

  • 2.1.1.在购物车页面上,点击删除

    /store/cart?method=remove&pid=??

<a href="javascript:void(0);" class="delete" onclick="removeFromCart('${item.product.pid}')">删除</a>

添加二次确认,location.href完成页面跳转

	<script type="text/javascript">
		function removeFromCart(pid){
			if(confirm("您确认狠心要丢弃我吗?")){
				location.href="${pageContext.request.contextPath}/cart?method=remove&pid="+pid;
			}
		}
	</script>

 

  • 2.1.2.在cartServlet中编写remove

    先获取商品的pid
    获取购物车
    删除购物车项

    重定向到/jsp/cart.jsp

	/**
	 * 从购物车中移除购物车项
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public String remove(HttpServletRequest request, HttpServletResponse response) throws Exception {
		//1.获取商品的pid
		String pid = request.getParameter("pid");
		
		//2.调用购物车的remove方法
		getCart(request).removeFromCart(pid);
		
		//3.重定向
		response.sendRedirect(request.getContextPath()+"/jsp/cart.jsp");
		return null;
	}

 

 

  • 三.案例3-对购物车进行操作(清空购物车)

  • 3.1.步骤分析

  • 3.1.1.在购物车页面上,有一个清空购物车的连接

    /store/cart?method=clear

<a href="${pageContext.request.contextPath }/cart?method=clear" id="clear" class="clear">清空购物车</a>
  • 3.1.2.在cartServlet中需要提供 clear

    先获取购物车
    调用Clearcart方法
    重定向 jsp/cart.jsp页面

	/**
	 * 清空购物车
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public String clear(HttpServletRequest request, HttpServletResponse response) throws Exception {
		//获取购物车 清空
		getCart(request).clearCart();
		response.sendRedirect(request.getContextPath()+"/jsp/cart.jsp");
		return null;
	}

 

    3.1.3.判断购物车是否为空

        若为空:提示购物车空空如也
        若不为空:展示

<c:if test="${empty cart.map }">
	<h1>购物车空空如也~~赶紧逛逛去!!</h1>
</c:if>
<c:if test="${not empty cart.map }">
	<div class="row">
		
		<div style="margin:0 auto; margin-top:10px;width:950px;">
			<strong style="font-size:16px;margin:5px 0;">订单详情</strong>
			<table class="table table-bordered">
				<tbody>
					<tr class="warning">
						<th>图片</th>
						<th>商品</th>
						<th>价格</th>
						<th>数量</th>
						<th>小计</th>
						<th>操作</th>
					</tr>
<c:forEach items="${cart.itmes }" var="item">
	<tr class="active">
		<td width="60" width="40%">
			<input type="hidden" name="id" value="22">
			<img src="${pageContext.request.contextPath}/${item.product.pimage}" width="70" height="60">
		</td>
		<td width="30%">
			<a target="_blank">${item.product.pname }</a>
		</td>
		<td width="20%">
			¥${item.product.shop_price }
		</td>
		<td width="10%">
			<input type="text" name="quantity" value="${item.count }" maxlength="4" size="10" readonly="readonly">
		</td>
		<td width="15%">
			<span class="subtotal">¥${item.subtotal }</span>
		</td>
		<td>
			<a href="javascript:void(0);" class="delete" onclick="removeFromCart('${item.product.pid}')">删除</a>
		</td>
	</tr>
</c:forEach>
				</tbody>
			</table>
		</div>
		
	</div>

	<div style="margin-right:130px;">
		<div style="text-align:right;">
			<em style="color:#ff6600;">
		登录后确认是否享有优惠&nbsp;&nbsp;
	</em> 赠送积分: <em style="color:#ff6600;">596</em>&nbsp; 商品金额: <strong style="color:#ff6600;">¥${cart.total }元</strong>
		</div>
		<div style="text-align:right;margin-top:10px;margin-bottom:10px;">
			<a href="${pageContext.request.contextPath }/cart?method=clear" id="clear" class="clear">清空购物车</a>
			<a href="${pageContext.request.contextPath }/order?method=add">
				<input type="submit" width="100" value="提交订单" name="submit" border="0" style="background: url('${pageContext.request.contextPath}/images/register.gif') no-repeat scroll 0 0 rgba(0, 0, 0, 0);
				height:35px;width:100px;color:white;">
			</a>
		</div>
	</div>
</c:if>

 

  • 四.案例4-生成订单

需求:
    在购物车页面上,有i一个提交订单,点击的时候,将用户购物车中的商品添加到数据库中.
实体:
    用户
    订单
    订单项(中间表)
    商品

  • 4.1.创建表

订单表
    CREATE TABLE `orders` (
      `oid` varchar(32) NOT NULL,
      `ordertime` datetime DEFAULT NULL,
      `total` double DEFAULT NULL,
      `state` int(11) DEFAULT NULL,
      `address` varchar(30) DEFAULT NULL,
      `name` varchar(20) DEFAULT NULL,
      `telephone` varchar(20) DEFAULT NULL,
      `uid` varchar(32) DEFAULT NULL,
      PRIMARY KEY (`oid`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
订单项表
    CREATE TABLE `orderitem` (
      `itemid` varchar(32) NOT NULL,
      `count` int(11) DEFAULT NULL,
      `subtotal` double DEFAULT NULL,
      `pid` varchar(32) DEFAULT NULL,
      `oid` varchar(32) DEFAULT NULL,
      PRIMARY KEY (`itemid`),
      KEY `fk_0001` (`pid`),
      KEY `fk_0002` (`oid`),
      CONSTRAINT `fk_0001` FOREIGN KEY (`pid`) REFERENCES `product` (`pid`),
      CONSTRAINT `fk_0002` FOREIGN KEY (`oid`) REFERENCES `orders` (`oid`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

  • 4.2.创建bean

    需要在order实体提供 user对象和list<OrderItem>

public class Order implements Serializable{

	private String oid;
	private Date ordertime;
	private Double total;
	
	private Integer state=0;//订单状态  0:未支付  1:已支付 
	private String address;
	private String name;
	
	private String telephone;
	
	//属于那个用户
	private User user;
	

	//包含那些订单项
	private List<OrderItem> items=new LinkedList<>();
	


    需要在orderItem实体中提供 product对象和order对象

public class OrderItem implements Serializable{

	
	private String itemid;
	private Integer count;
	private Double subtotal;
	
	//包含那个商品
	private Product product;
	
	//属于那个订单
	private Order order;

 

  • 4.3.步骤分析

  • 4.3.1.点击生成订单

    /store/order?method=add

<a href="${pageContext.request.contextPath }/order?method=add">
    <input type="submit" width="100" value="提交订单" name="submit" border="0" 
style="background: url('${pageContext.request.contextPath}/images/register.gif')
 no-repeat scroll 0 0 rgba(0, 0, 0, 0);
height:35px;width:100px;color:white;">
</a>

 

  • 4.3.2.创建OrderServlet

add方法中:
    1.判断用户是否登录,
    2.封装数据 Order
        oid            随机
        total        购物车中的总金额
        ordertime    当前时间
        
        User        session中当前用户
        
        订单项集合List<OrderItem>
            创建OrderItem,然后添加到list中
            orderItem从那里来????????/
                购物车中CartItem

        //1.4 订单的所有订单项
        /*
         * 先获取cart中itmes
         * 遍历itmes 组装成orderItem
         * 将orderItem添加到list(items)中
         */

        
        name(先不管)
        address(先不管)
        telephone(先不管)

/**
 * 订单模块
 */
public class OrderServlet extends BaseServlet {

	/**
	 * 生成订单
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception 
	 */
	public String add(HttpServletRequest request, HttpServletResponse response) throws Exception {
		//0.判断用户是否登录
		User user=(User) request.getSession().getAttribute("user");
		if(user == null){
			request.setAttribute("msg", "请先登录~~");
			return "/jsp/msg.jsp";
		}
		
		//1.封装数据
		Order order=new Order();
		//1.1 订单id
		order.setOid(UUIDUtils.getId());
		
		//1.2 订单时间
		order.setOrdertime(new Date());
		
		//1.3 总金额
		//获取session中cart
		Cart cart=(Cart) request.getSession().getAttribute("cart");
		
		order.setTotal(cart.getTotal());
		
		//1.4 订单的所有订单项
		/*
		 * 先获取cart中itmes
		 * 遍历itmes 组装成orderItem
		 * 将orderItem添加到list(items)中
		 */
		for (CartItem cartItem : cart.getItmes()) {
			OrderItem oi = new OrderItem();
			
			//设置id
			oi.setItemid(UUIDUtils.getId());
			
			//设置购买数量
			oi.setCount(cartItem.getCount());
			
			//设置小计
			oi.setSubtotal(cartItem.getSubtotal());
			
			//设置product
			oi.setProduct(cartItem.getProduct());
			
			//设置order
			oi.setOrder(order);
			
			//添加到list中
			order.getItems().add(oi);
		}
		
		//1.5 设置用户
		order.setUser(user);
		
		//2.调用service 添加订单
		OrderService os=(OrderService) BeanFactory.getBean("OrderService");
		os.add(order);
		
		//3.将order放入request域中,请求转发
		request.setAttribute("bean", order);
		
		//4.清空购物车
		request.getSession().removeAttribute("cart");
		return "/jsp/order_info.jsp";
	}

}

 

  • 4.3.3.调用orderservice 生成订单

        开启事务
        先往订单表中插入一条数据
        往订单项表中插入n条数据
        提交事务

public class OrderServiceImpl implements OrderService{

	@Override
	public void add(Order order) throws Exception{
		try {
			//1.开启事务
			DataSourceUtils.startTransaction();
			
			OrderDao od=(OrderDao) BeanFactory.getBean("OrderDao");
			//2.向orders表中添加一个数据
			od.add(order);
			
			//int i=1/0;
			
			//3.向orderitem中添加n条数据
			for (OrderItem oi : order.getItems()) {
				od.addItem(oi);
			}
			
			//4.事务处理
			DataSourceUtils.commitAndClose();
		} catch (Exception e) {
			e.printStackTrace();
			DataSourceUtils.rollbackAndClose();
			throw e;
		}
		
	}

}
  • dao中,qr使用空参构造器创建,在update中传入 conn

    QueryRunner qr = new QueryRunner();

    qr.update(DataSourceUtils.getConnection(),sql,...);

public class OrderDaoImpl implements OrderDao{

	/**
	 * 添加一条订单
	 */
	@Override
	public void add(Order order) throws Exception {
		QueryRunner qr = new QueryRunner();
		
		/*
		 * `oid` varchar(32) NOT NULL,
		  `ordertime` datetime DEFAULT NULL,
		  `total` double DEFAULT NULL,
		  
		  `state` int(11) DEFAULT NULL,
		  `address` varchar(30) DEFAULT NULL,
		  `name` varchar(20) DEFAULT NULL,
		  
		  `telephone` varchar(20) DEFAULT NULL,
		  `uid` varchar(32) DEFAULT NULL,
		 */
		String sql="insert into orders values(?,?,?,?,?,?,?,?)";
		qr.update(DataSourceUtils.getConnection(),sql, order.getOid(),order.getOrdertime(),order.getTotal(),order.getState(),
				order.getAddress(),order.getName(),order.getTelephone(),order.getUser().getUid());
	}

	/**
	 * 添加一条订单项
	 */
	@Override
	public void addItem(OrderItem oi) throws Exception {
		QueryRunner qr = new QueryRunner();
		 /**
		 * `itemid` varchar(32) NOT NULL,
		  `count` int(11) DEFAULT NULL,
		  `subtotal` double DEFAULT NULL,
		  `pid` varchar(32) DEFAULT NULL,
		  `oid` varchar(32) DEFAULT NULL,
		 */
		String sql="insert into orderitem values(?,?,?,?,?)";
		qr.update(DataSourceUtils.getConnection(),sql, oi.getItemid(),oi.getCount(),oi.getSubtotal(),oi.getProduct().getPid(),oi.getOrder().getOid());
	}

}

 

  • 4.4.数据库备份

图形化工具备份:
mysql数据库通过命令备份:不用登录数据库
    mysqldump -uroot -p1234 store28>g:\1.sql
mysql数据库通过命令还原:
    前提:手动的创建数据库
        方式1:不用登录数据库
            mysql -uroot -p1234 bak1<g:\1.sql
        方式2:需要登录到指定的数据库上
            source g:\1.sql 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
本课程详细讲解了以下内容:    1.jsp环境搭建及入门、虚拟路径和虚拟主机、JSP执行流程    2.使用Eclipse快速开发JSP、编码问题、JSP页面元素以及request对象、使用request对象实现注册示例    3.请求方式的编码问题、response、请求转发和重定向、cookie、session执行机制、session共享问题     4.session与cookie问题及application、cookie补充说明及四种范围对象作用域     5.JDBC原理及使用Statement访问数据库、使用JDBC切换数据库以及PreparedStatement的使用、Statement与PreparedStatement的区别     6.JDBC调用存储过程和存储函数、JDBC处理大文本CLOB及二进制BLOB类型数据     7.JSP访问数据库、JavaBean(封装数据和封装业务逻辑)     8.MVC模式与Servlet执行流程、Servlet25与Servlet30的使用、ServletAPI详解与源码分析     9.MVC案例、三层架构详解、乱码问题以及三层代码流程解析、完善Service和Dao、完善View、优化用户体验、优化三层(加入接口和DBUtil)    1 0.Web调试及bug修复、分页SQL(Oracle、MySQL、SQLSERVER)     11.分页业务逻辑层和数据访问层Service、Dao、分页表示层Jsp、Servlet     12.文件上传及注意问题、控制文件上传类型和大小、下载、各浏览器下载乱码问题     13.EL表达式语法、点操作符和中括号操作符、EL运算、隐式对象、JSTL基础及set、out、remove     14.过滤器、过滤器通配符、过滤器链、监听器     15.session绑定解绑、钝化活化     16.以及Ajax的各种应用     17. Idea环境下的Java Web开发

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值