MVC
MVC模式(Model-View-Controller)是软件工程中的一种软件架构模式,把软件系统分为三个基本部分:模型(Model)、视图(View)和控制器(Controller)。
MVC模式最早为Trygve Reenskaug提出,为施乐帕罗奥多研究中心(Xerox PARC)的Smalltalk语言发明的一种软件设计模式。
MVC可对程序的后期维护和扩展提供了方便,并且使程序某些部分的重用提供了方便。而且MVC也使程序简化,更加直观。
控制器Controller:对请求进行处理,负责请求转发;Servlet
特点:控制器接收用户输入并调用模型和视图去完成用户的需求
视图View:界面设计人员进行图形界面设计;JSP
特点:a、视图是用户看到并与之交互的界面
b、向用户显示相关的数据
c、接受用户的输入
d、不进行任何实际的业务处理
模型Model:程序编写程序应用的功能(实现算法等等)、数据库管理;Java对象
用于处理管理数据库、处理业务逻辑、封装实体数据 JavaBean
特点:一个模型能为多个视图提供数据
注意,MVC不是Java的东西,几乎现在所有B/S结构的软件都采用了MVC设计模式。但是要注意,MVC在B/S结构软件并没有完全实现,例如在我们今后的B/S软件中并不会有事件驱动!
实现MVC购物车的绑值和加入购物车初始版:
实体类和数据库辅助类该怎么写还是怎么写
//实体类:商品类
package com.zking.mvc.cart.entity;
import java.io.Serializable;
public class Goods implements Serializable{
private static final long serialVersionUID = -2858362891359151911L;
private int gid;
private String gname;
private int gprice;
private String ginfo;
private String gpath;
private int gkc;
private String gtype;
public Goods() {
// TODO Auto-generated constructor stub
}
public Goods(int gid, String gname, int gprice, String ginfo, String gpath, int gkc, String gtype) {
super();
this.gid = gid;
this.gname = gname;
this.gprice = gprice;
this.ginfo = ginfo;
this.gpath = gpath;
this.gkc = gkc;
this.gtype = gtype;
}
public Goods(String gname, int gprice, String ginfo, String gpath, int gkc, String gtype) {
super();
this.gname = gname;
this.gprice = gprice;
this.ginfo = ginfo;
this.gpath = gpath;
this.gkc = gkc;
this.gtype = gtype;
}
public int getGid() {
return gid;
}
public void setGid(int gid) {
this.gid = gid;
}
public String getGname() {
return gname;
}
public void setGname(String gname) {
this.gname = gname;
}
public int getGprice() {
return gprice;
}
public void setGprice(int gprice) {
this.gprice = gprice;
}
public String getGinfo() {
return ginfo;
}
public void setGinfo(String ginfo) {
this.ginfo = ginfo;
}
public String getGpath() {
return gpath;
}
public void setGpath(String gpath) {
this.gpath = gpath;
}
public int getGkc() {
return gkc;
}
public void setGkc(int gkc) {
this.gkc = gkc;
}
public String getGtype() {
return gtype;
}
public void setGtype(String gtype) {
this.gtype = gtype;
}
@Override
public String toString() {
return "Goods [gid=" + gid + ", gname=" + gname + ", gprice=" + gprice + ", ginfo=" + ginfo + ", gpath=" + gpath
+ ", gkc=" + gkc + ", gtype=" + gtype + "]";
}
}
//购物车类
package com.zking.mvc.cart.entity;
import java.io.Serializable;
public class Cart implements Serializable{
private static final long serialVersionUID = 5969840860422993319L;
private int cid;
private Goods goods;
private User user;
private int count;
private int ctotal;
public Cart() {
// TODO Auto-generated constructor stub
}
public int getCid() {
return cid;
}
public void setCid(int cid) {
this.cid = cid;
}
public Goods getGoods() {
return goods;
}
public void setGoods(Goods goods) {
this.goods = goods;
}
public User getUsers() {
return user;
}
public void setUsers(User user) {
this.user = user;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public int getCtotal() {
return ctotal;
}
public void setCtotal() {
this.ctotal = this.getGoods().getGprice() * this.getCount();
}
public Cart(int cid, Goods goods, User user, int count, int ctotal) {
this.cid = cid;
this.goods = goods;
this.user = user;
this.count = count;
this.ctotal = ctotal;
}
@Override
public String toString() {
return "Cart [cid=" + cid + ", goods=" + goods + ", users=" + user + ", count=" + count + ", ctotal=" + ctotal
+ "]";
}
}
三层架构该怎么写就怎么写
下面是servlet里面的代码用来实现绑值和加入购物车:
绑值代码:
package com.zking.mvc.cart.servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.zking.mvc.cart.biz.IGoodsBiz;
import com.zking.mvc.cart.biz.impl.GoodsBizImpl;
import com.zking.mvc.cart.entity.Goods;
import com.zking.mvc.cart.entity.User;
@WebServlet("/GoodsListServlet")
public class GoodsListServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//servlet作用:负责处理用户的请求以及响应
//1.设置字符编码
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("utf-8");
//2.获取参数 省略
//3.封装 调用biz判断
IGoodsBiz igb=new GoodsBizImpl();
List<Goods> listGoods = igb.queryGoodsAll();
//数据保存在域对象中
//获取域对象 session
HttpSession session = request.getSession();
session.setAttribute("listGoods", listGoods);
//获取当前项目的绝对路径
String path = request.getServletContext().getContextPath();// /web_mvc_cart_t280
//4.响应
response.sendRedirect("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">zkingzz购物首页</h2>
<hr/>
<p>欢迎您!大佬先生--${users.username}</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;
}
</script>
</body>
</html>
加入购物车:
package com.zking.mvc.cart.servlet;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.zking.mvc.cart.biz.ICartBiz;
import com.zking.mvc.cart.biz.IGoodsBiz;
import com.zking.mvc.cart.biz.impl.CartBizImpl;
import com.zking.mvc.cart.biz.impl.GoodsBizImpl;
import com.zking.mvc.cart.entity.Cart;
import com.zking.mvc.cart.entity.Goods;
import com.zking.mvc.cart.entity.User;
@WebServlet("/AddCartServlet")
public class AddCartServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//jrebel 热部署插件
//加入加购物车的servlet
//System.out.println("进来了!");
//1.设置字符集编码
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
//2.获取location带过来的gid
String id = request.getParameter("gid");
int gid = 0;
if(null!=id) {
gid = Integer.valueOf(id);
}
//数据封装
Cart c = new Cart();
//将gid Userid ccount ctotal 封装到cart实体对象中
IGoodsBiz igb = new GoodsBizImpl();
Goods goods = igb.getGoodsByGid(gid);
//userid
HttpSession session = request.getSession();
User user = (User)session.getAttribute("users");
c.setGoods(goods);
c.setUsers(user);
c.setCount(1);
c.setCtotal();
//System.out.println(c);
//要想将封装好的购物车信息 保存到数据库中
//先获取当前这个登录用户的购物车
ICartBiz icb = new CartBizImpl();
List<Cart> listCarts = icb.getCartByUsersid(user.getUserid());
//如果购物车List<Cart> listCarts = null;
boolean flag = true;
//如果该用户第一次进入 构建一个新的购物车 实例化
if(null == listCarts ) {
listCarts = new ArrayList<Cart>();
}
else {//非第一次
//遍历所有的商品
for (Cart cart : listCarts) {
//判断当前加入的与购物车已有的进行匹配
if(cart.getGoods().getGid() == c.getGoods().getGid()) {
//不需要重复添加 修改数量和总价格
cart.setCount(cart.getCount()+1);
cart.setCtotal();
//进行一个数据库的更新
icb.editCartByCidAndUserid(cart);
flag = false;
break;
}
}
}
//问题:加入购物车 不是加入集合 而是加到数据库
//listCarts.add(c);
if(flag) {
icb.addCart(c);
}
//加入后数据要继续更新
listCarts = icb.getCartByUsersid(user.getUserid());
session.setAttribute("listCarts", listCarts);
response.sendRedirect("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>${goods.ccount}</td>
<td>${goods.ctotal}</td>
<td>
<button>删除</button><button>修改</button>
</td>
</tr>
</c:forEach>
</table>
</body>
</html>
以上就是初始版的MVC购物车项目