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&