javaweb之MVC购物车(加入购物车,订单,订单详情)

MVC(Model View Controller)是软件工程中的一种软件架构模式,它把软件系统分为模型、视图和控制器三个基本部分。用一种业务逻辑、数据、界面显示分离的方法组织代码,将业务逻辑聚集到一个部件里面,在改进和个性化定制界面及用户交互的同时,不需要重新编写业务逻辑。

用MVC写购物车的优点:
1. 耦合性低,方便维护,可以利于分工协作

2. 重用性高
 


1、index.jsp(主界面)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <!--通过taglib引入JSTL标准标签库  -->
    <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<!--通过c标签判断是否在域对象中存在数据-->
	<c:if test="${empty listGoods }">
		<jsp:forward page="GoodsListServlet"></jsp:forward>
	</c:if>

	<h2 style="text-align:center">${users.username}购物首页</h2>
	<hr/>
	<p>欢迎您!大佬先生--${users.username}
	<button onclick="tc()">退出登录</button>
	</p>
	<table border="1" cellspacing="0" cellpadding="0" width="100%" >
		<tr>
			<th>商品编码</th>
			<th>商品名称</th>
			<th>商品价格</th>
			<th>操作</th>
		</tr>
		<!--通过c标签遍历数据  -->
		<c:forEach items="${listGoods}" var="goods">
			<tr>
				<td>${goods.gid}</td>
				<td>${goods.gname}</td>
				<td>${goods.gprice}</td>
				<td>
					<button onclick="addCart(${goods.gid})">加入购物车</button>
				</td>
			</tr>
		</c:forEach>
	</table>
	
	<script type="text/javascript">
	//加入购物车的点击事件
	function addCart(gid) {
		//alert(gid);
		window.location.href="AddCartServlet?gid="+gid;
	}
	
	function tc() {
		window.location.href="DownLoadServlt";
	}
	
	</script>
</body>
</html>

效果图:

 cart.jsp(我的购物车)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h1>我的购物车</h1>
	<p><a href="index.jsp">返回首页</a></p>
	<hr/>
	<table border="1" cellspacing="0" cellpadding="0" width="100%" >
		<tr>
			<th>购物车编码</th>
			<th>商品名称</th>
			<th>商品单价</th>
			<th>商品数量</th>
			<th>商品总价</th>
			<th>操作</th>
		</tr>
		<!--通过c标签遍历数据  -->
		<c:forEach items="${listCarts}" var="cart">
			<tr>
				<td>${cart.cid}</td>
				<td>${cart.goods.gname}</td>
				<td>${cart.goods.gprice}</td>
				<td>${cart.count}</td>
				<td>${cart.ctotal}</td>
				<td>
					<button>删除</button><button>修改</button>
				</td>
			</tr>
		</c:forEach>
	</table>
	
	<p style="text-align: right" onclick="buy()">
		<button>结算</button>
	</p>
	
	<script type="text/javascript">
	
		function buy() {
			location.href="FindCartInfoServlet";
		}
	
	
	
	</script>
	
	
	
	
</body>
</html>

效果图:

 3.cartfirm.jsp(订单界面)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h3>确认订单</h3>
	<h6>确认收货地址</h6>
	<input id="oaddress" type="text" style="width: 100%;height: 40px"/>
	<br/><br/>
	<hr/>
	<h6>确认订单信息</h6>
	<p>
		商品总件数:${sessionScope.maps.sumCount }
	</p>
	
	<h6>商品信息</h6>
	<c:forEach items="${sessionScope.maps.cartByUsersid }" var="mycar">
		<p>
			<span&
  • 7
    点赞
  • 76
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值