一、请简述你设计的商品列表功能。
1.该功能创建了几张表,每个表的功能是什么,并介绍下主要字段和作用?
recommend表对应商品自动滑动的,表里包含自身的id和蛋糕goods的id,蛋糕所属类别的id;
type表对应蛋糕所属类别的id和名称;
goods表对应所有商品的内容,包含商品id,名称,商品图片,商品价格,商品介绍,商品库存,蛋糕所属类别id;
2.简要说明实现该功能。比如:创建了几个类,并介绍功能、算法逻辑、实现代码。
算法实现:
首先登录蛋糕商城首页是通过index.jsp展示的,其中的目录是通过引入header.jsp,底部联系网址是通过引入footer.jsp;页面的蛋糕内容是通过IndexServlet.class类中doGet方法来获取数据库内容(其中IndexServlet类是用来填写数据,GoodsService类是实现数据库每一条语句对应一个方法,相当于mybatis中的接口,GoodsDao类是实现GoodsService中的方法),然后填充到页面中,再通过index.jsp进行展示实现的;
关键代码:
List<Map<String,Object>> ScrollGood=gService.getScrollGood();
request.setAttribute("scroll",ScrollGood);
List<Map<String,Object>>newList=gService.getGoodsList(3);
request.setAttribute("newList",newList);
List<Map<String,Object>>hotList=gService.getGoodsList(2);
request.setAttribute("hotList",hotList);
//response.sendRedirect("index.jsp");
request.getRequestDispatcher("index.jsp").forward(request,response);
Goods类实现goods实例化;
public class Goods {
private int id;
private String name;
private String cover;
private String image1;
private String image2;
private float price;
private String intro;
private int stock;
private Type type;
private boolean isScroll;
private boolean isHot;
private boolean isNew;
public boolean getIsScroll() {
return isScroll;
}
public void setScroll(boolean isScroll) {
this.isScroll = isScroll;
}
public boolean getIsHot() {
return isHot;
}
public void setHot(boolean isHot) {
this.isHot = isHot;
}
public boolean getIsNew() {
return isNew;
}
public void setNew(boolean isNew) {
this.isNew = isNew;
}
public void setTypeid(int typeid) {
if(type==null) {
type = new Type();
}
type.setId(typeid);
}
public void setTypename(String typename) {
if(type==null) {
type = new Type();
}
type.setName(typename);
}
@Override
public String toString() {
return "Goods [id=" + id + ", name=" + name + ", cover=" + cover + ", image1=" + image1 + ", image2=" + image2
+ ", price=" + price + ", intro=" + intro + ", stock=" + stock + ", type=" + type + "]";
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCover() {
return cover;
}
public void setCover(String cover) {
this.cover = cover;
}
public String getImage1() {
return image1;
}
public void setImage1(String image1) {
this.image1 = image1;
}
public String getImage2() {
return image2;
}
public void setImage2(String image2) {
this.image2 = image2;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public String getIntro() {
return intro;
}
public void setIntro(String intro) {
this.intro = intro;
}
public int getStock() {
return stock;
}
public void setStock(int stock) {
this.stock = stock;
}
public Type getType() {
return type;
}
public void setType(Type type) {
this.type = type;
}
public Goods() {
super();
}
public Goods(int id, String name, String cover, String image1, String image2, float price, String intro, int stock,
Type type) {
super();
this.id = id;
this.name = name;
this.cover = cover;
this.image1 = image1;
this.image2 = image2;
this.price = price;
this.intro = intro;
this.stock = stock;
this.type = type;
}
}
Recommend类实现recommend表实例化;
public class Recommend {
private int id;
private int type;//1条幅 2热销 3新品
private Goods goods;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public Goods getGoods() {
return goods;
}
public void setGoods(Goods goods) {
this.goods = goods;
}
public Recommend(int id, int type, Goods goods) {
super();
this.id = id;
this.type = type;
this.goods = goods;
}
public Recommend() {
super();
}
}
Type类实现type表的实例化;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
public class Type {
private int id;
private String name;
private String encodeName;
public String getEncodeName() {
return encodeName;
}
public void setEncodeName(String encodeName) {
this.encodeName = encodeName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
try {
this.encodeName = URLEncoder.encode(name, "utf-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Type(int id, String name) {
super();
this.id = id;
this.name = name;
}
public Type() {
super();
}
public Type(String name) {
super();
this.name = name;
}
}
GoodsDao类实现对goods表的进行增删改查;
import model.Goods;
import model.Recommend;
import org.apache.commons.dbutils.*;
import org.apache.commons.dbutils.handlers.*;
import utils.DataSourceUtils;
import java.sql.SQLException;
import java.util.*;
public class GoodsDao {
//select g.id,g.name,g.cover,g.price,t.name typename from recommend r,goods g,type t where type=2 and r.goods_id=g.id and g.type_id=t.id
public List<Map<String,Object>> getGoodsList(int recommendType) throws SQLException {
QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());
String sql="select g.id,g.name,g.cover,g.price,t.name typename from recommend r,goods g,type t where type=? and r.goods_id=g.id and g.type_id=t.id";
return r.query(sql, new MapListHandler(),recommendType);
}
public List<Map<String,Object>> getScrollGood()throws SQLException{
QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());
// String sql="select g.id,g.name,g.cover,g.price from recommend r,goods g where type=1 and r.goods_id=g.id";
// return r.query(sql, new MapHandler());
String sql="select g.id,g.name,g.cover,g.price from recommend r,goods g where r.goods_id=g.id";
return r.query(sql, new MapListHandler());
}
public List<Goods> selectGoodsByTypeID(int typeID,int pageNumber,int pageSize) throws SQLException {
if(typeID==0)
{
String sql="select * from goods limit ? , ?";
QueryRunner r=new QueryRunner(DataSourceUtils.getDataSource());
return r.query(sql,new BeanListHandler<Goods>(Goods.class),(pageNumber-1)*pageSize,pageSize);
}
else
{
String sql="select * from goods where type_id=? limit ? , ?";
QueryRunner r=new QueryRunner(DataSourceUtils.getDataSource());
return r.query(sql,new BeanListHandler<Goods>(Goods.class),typeID,(pageNumber-1)*pageSize,pageSize);
}
}
public int getCountOfGoodsByTypeID(int typeID) throws SQLException {
String sql="";
QueryRunner r=new QueryRunner(DataSourceUtils.getDataSource());
if(typeID==0)
{
sql="select count(*) from goods";
return r.query(sql,new ScalarHandler<Long>()).intValue();
}
else
{
sql="select count(*) from goods where type_id=?";
return r.query(sql,new ScalarHandler<Long>(),typeID).intValue();
}
}
public List<Goods> selectGoodsbyRecommend(int type,int pageNumber,int pageSize) throws SQLException {
QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());
if(type==0) {
//当不添加推荐类型限制的时候
String sql = " select g.id,g.name,g.cover,g.image1,g.image2,g.intro,g.price,g.stock,t.name typename from goods g,type t where g.type_id=t.id order by g.id limit ?,?";
return r.query(sql, new BeanListHandler<Goods>(Goods.class),(pageNumber-1)*pageSize,pageSize);
}
String sql = " select g.id,g.name,g.cover,g.image1,g.image2,g.intro,g.price,g.stock,t.name typename from goods g,recommend r,type t where g.id=r.goods_id and g.type_id=t.id and r.type=? order by g.id limit ?,?";
return r.query(sql, new BeanListHandler<Goods>(Goods.class),type,(pageNumber-1)*pageSize,pageSize);
}
public int getRecommendCountOfGoodsByTypeID(int type) throws SQLException {
if(type==0)return getCountOfGoodsByTypeID(0);
QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "select count(*) from recommend where type=?";
return r.query(sql, new ScalarHandler<Long>(),type).intValue();
}
public Goods getGoodsById(int id) throws SQLException {
QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "select g.id,g.name,g.cover,g.image1,g.image2,g.price,g.intro,g.stock,t.id typeid,t.name typename from goods g,type t where g.id = ? and g.type_id=t.id";
return r.query(sql, new BeanHandler<Goods>(Goods.class),id);
}
public int getSearchCount(String keyword) throws SQLException {
QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "select count(*) from goods where name like ?";
return r.query(sql, new ScalarHandler<Long>(),"%"+keyword+"%").intValue();
}
public List<Goods> selectSearchGoods(String keyword, int pageNumber, int pageSize) throws SQLException{
QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "select * from goods where name like ? limit ?,?";
return r.query(sql, new BeanListHandler<Goods>(Goods.class),"%"+keyword+"%",(pageNumber-1)*pageSize,pageSize);
}
public boolean isScroll(Goods g) throws SQLException {
return isRecommend(g, 1);
}
public boolean isHot(Goods g) throws SQLException {
return isRecommend(g, 2);
}
public boolean isNew(Goods g) throws SQLException {
return isRecommend(g, 3);
}
private boolean isRecommend(Goods g,int type) throws SQLException {
QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "select * from recommend where type=? and goods_id=?";
Recommend recommend = r.query(sql, new BeanHandler<Recommend>(Recommend.class),type,g.getId());
if(recommend==null) {
return false;
}else {
return true;
}
}
public void addRecommend(int id,int type) throws SQLException {
QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "insert into recommend(type,goods_id) values(?,?)";
r.update(sql,type,id);
}
public void removeRecommend(int id,int type) throws SQLException {
QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "delete from recommend where type=? and goods_id=?";
r.update(sql,type,id);
}
public void insert(Goods g) throws SQLException {
QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "insert into goods(name,cover,image1,image2,price,intro,stock,type_id) values(?,?,?,?,?,?,?,?)";
r.update(sql,g.getName(),g.getCover(),g.getImage1(),g.getImage2(),g.getPrice(),g.getIntro(),g.getStock(),g.getType().getId());
}
public void update(Goods g) throws SQLException {
QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "update goods set name=?,cover=?,image1=?,image2=?,price=?,intro=?,stock=?,type_id=? where id=?";
r.update(sql,g.getName(),g.getCover(),g.getImage1(),g.getImage2(),g.getPrice(),g.getIntro(),g.getStock(),g.getType().getId(),g.getId());
}
public void delete(int id) throws SQLException {
QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "delete from goods where id = ?";
r.update(sql,id);
}
}
TypeDao类实现对type表的增上改查来辅助GoodsDao更好的对goods类别的分类;
import model.Type;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import utils.DataSourceUtils;
import java.sql.SQLException;
import java.util.List;
public class TypeDao
{
public List<Type> GetAllType() throws SQLException {
QueryRunner r=new QueryRunner(DataSourceUtils.getDataSource());
String sql="select * from type";
return r.query(sql,new BeanListHandler<Type>(Type.class));
}
public Type selectTypeNameByID(int typeid) throws SQLException {
QueryRunner r=new QueryRunner(DataSourceUtils.getDataSource());
String sql="select * from type where id=?";
return r.query(sql,new BeanHandler<Type>(Type.class),typeid);
}
public Type select(int id) throws SQLException {
QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "select * from type where id = ?";
return r.query(sql, new BeanHandler<Type>(Type.class),id);
}
public void insert(Type t) throws SQLException {
QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "insert into type(name) values(?)";
r.update(sql,t.getName());
}
public void update(Type t) throws SQLException {
QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "update type set name=? where id = ?";
r.update(sql,t.getName(),t.getId());
}
public void delete(int id) throws SQLException {
QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "delete from type where id = ?";
r.update(sql,id);
}
}
Index.jsp蛋糕商城的首页,展示蛋糕种类;
<%@ page contentType="text/html;charset=UTF-8"
language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path=request.getContextPath();
String basePath=request.getScheme()+"://"+request.getServerName()+":8080"+path+"/";
%>
<!DOCTYPE html>
<html>
<head>
<title>商品列表</title>
<base href="<%=basePath%>">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link type="text/css" rel="stylesheet" href="css/bootstrap.css">
<link type="text/css" rel="stylesheet" href="css/style.css">
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/bootstrap.min.js"></script>
<script type="text/javascript" src="layer/layer.js"></script>
<script type="text/javascript" src="js/cart.js"></script>
</head>
<body>
<!--header-->
<jsp:include page="/header.jsp">
<jsp:param name="flag" value="1"></jsp:param>
</jsp:include>
<!--banner-->
<div class="banner">
<div class="container">
<div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<ol class="carousel-indicators" id="olnum">
<c:forEach items="${scroll}" var="g" varStatus="status">
<c:choose>
<c:when test="${status.first}">
<li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
</c:when>
<c:otherwise>
<li data-target="#carousel-example-generic" data-slide-to="${status.index}"></li>
</c:otherwise>
</c:choose>
</c:forEach>
</ol>
<!-- Wrapper for slides -->
<div class="carousel-inner" role="listbox" id="lunbotu" style="width: 1242px; height: 432px;">
<c:forEach items="${scroll}" var="g" varStatus="status">
<c:choose>
<c:when test="${status.first}">
<div class="item active">
<h2 class="hdng"><a href="goods_detail?id=${g.id}">${g.name}</a><span></span></h2>
<p>今日精选推荐</p>
<a class="banner_a" href="javascript:;" onclick="buy(${g.id})">立刻购买</a>
<div class="banner-text">
<a href="goods_detail?id=${g.id}">
<img src="${g.cover}" alt="${g.name}" width="350" height="350">
</a>
</div>
</div>
</c:when>
<c:otherwise>
<div class="item">
<h2 class="hdng"><a href="goods_detail?id=${g.id}">${g.name}</a><span></span></h2>
<p>今日精选推荐</p>
<a class="banner_a" href="javascript:;" onclick="buy(${g.id})">立刻购买</a>
<div class="banner-text">
<a href="goods_detail?id=${g.id}">
<img src="${g.cover}" alt="${g.name}" width="350" height="350">
</a>
</div>
</div>
</c:otherwise>
</c:choose>
</c:forEach>
</div>
<!-- Controls -->
<%--<a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>--%>
</div>
</div>
</div>
<!--//banner-->
<div class="subscribe2"></div>
<!--gallery-->
<div class="gallery">
<div class="container">
<div class="alert alert-danger">热销推荐</div>
<div class="gallery-grids">
<c:forEach items="${hotList}" var="g">
<div class="col-md-4 gallery-grid glry-two">
<a href="goods_detail?id=${g.id}">
<img src="${g.cover}" class="img-responsive" alt="${g.name}" width="350" height="350"/>
</a>
<div class="gallery-info galrr-info-two">
<p>
<span class="glyphicon glyphicon-eye-open" aria-hidden="true"></span>
<a href="goods_detail?id=${g.id}">查看详情</a>
</p>
<a class="shop" href="javascript:;" onclick="buy(${g.id})">立刻购买</a>
<div class="clearfix"></div>
</div>
<div class="galy-info">
<p>${g.typeName} > ${g.name}</p>
<div class="galry">
<div class="prices">
<h5 class="item_price">¥ ${g.price}</h5>
</div>
<div class="clearfix"></div>
</div>
</div>
</div>
</c:forEach>
</div>
<div class="clearfix"></div>
<div class="alert alert-info">新品推荐</div>
<div class="gallery-grids">
<c:forEach items="${newList}" var="g">
<div class="col-md-3 gallery-grid ">
<a href="goods_detail?id=${g.id}">
<img src="${g.cover}" class="img-responsive" alt="${g.name}"/>
</a>
<div class="gallery-info">
<p>
<span class="glyphicon glyphicon-eye-open" aria-hidden="true"></span>
<a href="goods_detail?id=${g.id}">查看详情</a>
</p>
<a class="shop" href="javascript:;" onclick="buy(${g.id})">立刻购买</a>
<div class="clearfix"></div>
</div>
<div class="galy-info">
<p>${g.typeName} > ${g.name}</p>
<div class="galry">
<div class="prices">
<h5 class="item_price">¥ ${g.price}</h5>
</div>
<div class="clearfix"></div>
</div>
</div>
</div>
</c:forEach>
</div>
</div>
</div>
<!--//gallery-->
<!--subscribe-->
<div class="subscribe"></div>
<!--//subscribe-->
<!--footer-->
<jsp:include page="/footer.jsp"></jsp:include>
</body>
</html>
footer.jsp商城页面最下面内容显示
<%@ page contentType="text/html;charset=UTF-8"
language="java" %>
<!--footer-->
<div class="footer">
<div class="container">
<div class="text-center">
<p>vili www.vilicode.com © All rights Reseverd</p>
</div>
</div>
</div>
header.jsp实现蛋糕商城首页上面目录显示;
<%@ page contentType="text/html;charset=UTF-8"
language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
Created by IntelliJ IDEA.
User: 19767
Date: 2018/11/23
Time: 15:49
To change this template use File | Settings | File Templates.
--%>
<!--header-->
<div class="header">
<div class="container">
<nav class="navbar navbar-default" role="navigation">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<h1 class="navbar-brand"><a href="/index"></a></h1>
</div>
<!--navbar-header-->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li><a href="/index" <c:if test="${param.flag==1}">class="active"</c:if>>首页</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle <c:if test="${param.flag==2}">active</c:if>" data-toggle="dropdown">商品分类<b class="caret"></b></a>
<ul class="dropdown-menu multi-column columns-2">
<li>
<div class="row">
<div class="col-sm-12">
<h4>商品分类</h4>
<ul class="multi-column-dropdown">
<li><a class="list" href="/goods_list">全部系列</a></li>
<c:forEach items="${typeList}" var="t">
<li><a class="list" href="/goods_list?typeid=${t.id}">${t.name}</a></li>
</c:forEach>
</ul>
</div>
</div>
</li>
</ul>
</li>
<li><a href="/goodsrecommend_list?type=2" <c:if test="${param.flag==3 && t==2}">class="active"</c:if>>热销</a></li>
<li><a href="/goodsrecommend_list?type=3" <c:if test="${param.flag==3 && t==3}">class="active"</c:if>>新品</a></li>
<c:choose><c:when test="${empty user }">
<li><a href="/user_register.jsp" <c:if test="${param.flag==10 }">class="active"</c:if>>注册</a></li>
<li><a href="/user_login.jsp" <c:if test="${param.flag==9 }">class="active"</c:if>>登录</a></li>
</c:when><c:otherwise>
<li><a href="/order_list" <c:if test="${param.flag==5 }">class="active"</c:if>>我的订单</a></li>
<li><a href="/user_center.jsp" <c:if test="${param.flag==4 }">class="active"</c:if>>个人中心</a></li>
<li><a href="/user_logout" >退出</a></li>
</c:otherwise>
</c:choose>
<c:if test="${!empty user && user.isadmin }">
<li><a href="/admin/index.jsp" target="_blank">后台管理</a></li>
</c:if>
</ul>
<!--/.navbar-collapse-->
</div>
<!--//navbar-header-->
</nav>
<div class="header-info">
<div class="header-right search-box">
<a href="javascript:;"><span class="glyphicon glyphicon-search" aria-hidden="true"></span></a>
<div class="search">
<form class="navbar-form" action="/goods_search">
<input type="text" class="form-control" name="keyword">
<button type="submit" class="btn btn-default <c:if test="${param.flag==7 }">active</c:if>" aria-label="Left Align">搜索</button>
</form>
</div>
</div>
<div class="header-right cart">
<a href="goods_cart.jsp">
<span class="glyphicon glyphicon-shopping-cart <c:if test="${param.flag==8 }">active</c:if>" aria-hidden="true"><span class="card_num"><c:choose><c:when test="${empty order}">0</c:when><c:otherwise>${order.amount}</c:otherwise></c:choose></span></span>
</a>
</div>
<div class="clearfix"> </div>
</div>
<div class="clearfix"> </div>
</div>
</div>
<!--//header-->
3.该商品列表运行截图。
1.普通用户
首页
文章来源地址https://www.toymoban.com/news/detail-767185.html
用户登录
购物车
支付
2.管理员登录
首页
订单管理
客户管理
商品管理
类型管理
二、请简述你设计的订单功能。
1.该功能创建了几张表,每个表的功能是什么,并介绍下主要字段和作用?
User表实现用户和管理员登录的账号;username用户登录名,password登录密码,name用户属性,phone电话号码,adress用户收货地址,isadmin是否为管理员;
Order表管理员对用户订单的管理,total表示订单总价格,amount一个订单蛋糕数量,status表示用户订单是否发货,paytype用户支付类型,name收货人名字,phone收货人电话,adress收货地址,datetime购买时间,user_id购买人的id;
Orderitem表实现用户订单和管理员订单内容管理;prince订单中蛋糕的价格,amount蛋糕购买的数量,goods_id商品的id,order_id订单的id。
2.简要说明实现该功能。比如:创建了几个类,并介绍功能、算法逻辑、实现代码。
算法实现
用户订单
Order类对订单属性的实例化;OrderListServlet类首先获取页面中用户的id,如果没有出现登录界面;然后通过OrderService类中的方法来获取OrderDao中的sql语句查询数据库的信息来填写页面的内容,然后跳转到订单界面。
管理员订单
Page类管理订单页面;AdminOrderListServlet类首先判断用户的订单是否发货,然后通过OrderService类中的方法来获取OrderDao中的sql语句查询数据库的信息来填写页面的内容,然后跳转到订单管理界面。
3.该订单运行截图。
用户订单
管理员订单
代码
Page.class
import java.util.List;
public class Page {
private int pageNumber;
private int pageSize;
private int totalCount;
private int totalPage;
private List<Object> list;
public void SetPageSizeAndTotalCount(int pageSize,int totalCount)
{
this.pageSize=pageSize;
this.totalCount=totalCount;
totalPage= (int)Math.ceil((double)totalCount/pageSize);
}
public int getPageNumber() {
return pageNumber;
}
public void setPageNumber(int pageNumber) {
this.pageNumber = pageNumber;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public List<Object> getList() {
return list;
}
public void setList(List<Object> list) {
this.list = list;
}
}
AdminOrderListServlet.class
import model.Page;
import service.OrderService;
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 java.io.IOException;
@WebServlet(name = "admin_order_list",urlPatterns = "/admin/order_list")
public class AdminOrderListServlet extends HttpServlet {
private OrderService oService = new OrderService();
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int status = 0;
if(request.getParameter("status") != null) {
status=Integer.parseInt(request.getParameter("status") ) ;
}
request.setAttribute("status", status);
int pageNumber = 1;
if(request.getParameter("pageNumber") != null) {
try {
pageNumber=Integer.parseInt(request.getParameter("pageNumber") ) ;
}
catch (Exception e)
{
}
}
if(pageNumber<=0)
pageNumber=1;
Page p = oService.getOrderPage(status,pageNumber);
if(p.getTotalPage()==0)
{
p.setTotalPage(1);
p.setPageNumber(1);
}
else {
if(pageNumber>=p.getTotalPage()+1)
{
p = oService.getOrderPage(status,pageNumber);
}
}
request.setAttribute("p", p);
request.getRequestDispatcher("/admin/order_list.jsp").forward(request, response);
}
}
OrderService.class
import dao.*;
import model.*;
import utils.*;
import java.sql.*;
import java.util.List;
public class OrderService {
private OrderDao oDao = new OrderDao();
public void addOrder(Order order) {
Connection con = null;
try {
con = DataSourceUtils.getConnection();
con.setAutoCommit(false);
oDao.insertOrder(con, order);
int id = oDao.getLastInsertId(con);
order.setId(id);
for(OrderItem item : order.getItemMap().values()) {
oDao.insertOrderItem(con, item);
}
con.commit();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
if(con!=null)
try {
con.rollback();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
public List<Order> selectAll(int userid){
List<Order> list=null;
try {
list = oDao.selectAll(userid);
for(Order o :list) {
List<OrderItem> l = oDao.selectAllItem(o.getId());
o.setItemList(l);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
public Page getOrderPage(int status,int pageNumber) {
Page p = new Page();
p.setPageNumber(pageNumber);
int pageSize = 10;
int totalCount = 0;
try {
totalCount = oDao.getOrderCount(status);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
p.SetPageSizeAndTotalCount(pageSize, totalCount);
List list=null;
try {
list = oDao.selectOrderList(status, pageNumber, pageSize);
for(Order o :(List<Order>)list) {
List<OrderItem> l = oDao.selectAllItem(o.getId());
o.setItemList(l);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
p.setList(list);
return p;
}
public void updateStatus(int id,int status) {
try {
oDao.updateStatus(id, status);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void delete(int id) {
Connection con = null;
try {
con = DataSourceUtils.getDataSource().getConnection();
con.setAutoCommit(false);
oDao.deleteOrderItem(con, id);
oDao.deleteOrder(con, id);
con.commit();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
if(con!=null)
try {
con.rollback();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
OrderDao.class
import model.*;
import org.apache.commons.dbutils.*;
import utils.*;
import java.math.*;
import java.sql.*;
import java.util.*;
import org.apache.commons.dbutils.handlers.*;
public class OrderDao {
public void insertOrder(Connection con, Order order) throws SQLException {
QueryRunner r = new QueryRunner();
String sql = "insert into `order`(total,amount,status,paytype,name,phone,address,datetime,user_id) values(?,?,?,?,?,?,?,?,?)";
r.update(con,sql,
order.getTotal(),order.getAmount(),order.getStatus(),
order.getPaytype(),order.getName(),order.getPhone(),
order.getAddress(),order.getDatetime(),order.getUser().getId() );
}
public int getLastInsertId(Connection con) throws SQLException {
QueryRunner r = new QueryRunner();
String sql = "select last_insert_id()";
BigInteger bi = r.query(con, sql,new ScalarHandler<BigInteger>());
return Integer.parseInt(bi.toString());
}
public void insertOrderItem(Connection con, OrderItem item) throws SQLException {
QueryRunner r = new QueryRunner();
String sql ="insert into orderitem(price,amount,goods_id,order_id) values(?,?,?,?)";
r.update(con,sql,item.getPrice(),item.getAmount(),item.getGoods().getId(),item.getOrder().getId());
}
public List<Order> selectAll(int userid) throws SQLException {
QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "select * from `order` where user_id=? order by datetime desc";
return r.query(sql, new BeanListHandler<Order>(Order.class),userid);
}
public List<OrderItem> selectAllItem(int orderid) throws SQLException{
QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "select i.id,i.price,i.amount,g.name from orderitem i,goods g where order_id=? and i.goods_id=g.id";
return r.query(sql, new BeanListHandler<OrderItem>(OrderItem.class),orderid);
}
public int getOrderCount(int status) throws SQLException {
QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "";
if(status==0) {
sql = "select count(*) from `order`";
return r.query(sql, new ScalarHandler<Long>()).intValue();
}else {
sql = "select count(*) from `order` where status=?";
return r.query(sql, new ScalarHandler<Long>(),status).intValue();
}
}
public List<Order> selectOrderList(int status, int pageNumber, int pageSize) throws SQLException {
QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());
if(status==0) {
String sql = "select o.id,o.total,o.amount,o.status,o.paytype,o.name,o.phone,o.address,o.datetime,u.username from `order` o,user u where o.user_id=u.id order by o.datetime desc limit ?,?";
return r.query(sql, new BeanListHandler<Order>(Order.class), (pageNumber-1)*pageSize,pageSize );
}else {
String sql = "select o.id,o.total,o.amount,o.status,o.paytype,o.name,o.phone,o.address,o.datetime,u.username from `order` o,user u where o.user_id=u.id and o.status=? order by o.datetime desc limit ?,?";
return r.query(sql, new BeanListHandler<Order>(Order.class),status, (pageNumber-1)*pageSize,pageSize );
}
}
public void updateStatus(int id,int status) throws SQLException {
QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());
String sql ="update `order` set status=? where id = ?";
r.update(sql,status,id);
}
public void deleteOrder(Connection con ,int id) throws SQLException {
QueryRunner r = new QueryRunner();
String sql ="delete from `order` where id = ?";
r.update(con,sql,id);
}
public void deleteOrderItem(Connection con ,int id) throws SQLException {
QueryRunner r = new QueryRunner();
String sql ="delete from orderitem where order_id=?";
r.update(con,sql,id);
}
}
order_list.jsp
<%@ page contentType="text/html;charset=UTF-8"
language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<title>我的订单</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link type="text/css" rel="stylesheet" href="css/bootstrap.css">
<link type="text/css" rel="stylesheet" href="css/style.css">
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/bootstrap.min.js"></script>
<script type="text/javascript" src="layer/layer.js"></script>
<script type="text/javascript" src="js/cart.js"></script>
</head>
<body>
<!--header-->
<jsp:include page="header.jsp">
<jsp:param name="flag" value="5"></jsp:param>
</jsp:include>
<!--//header-->
<!--cart-items-->
<div class="cart-items">
<div class="container">
<h2>我的订单</h2>
<table class="table table-bordered table-hover">
<tr>
<th width="10%">ID</th>
<th width="10%">总价</th>
<th width="20%">商品详情</th>
<th width="30%">收货信息</th>
<th width="10%">订单状态</th>
<th width="10%">支付方式</th>
<th width="10%">下单时间</th>
</tr>
<c:forEach items="${orderList }" var="order">
<tr>
<td><p>${order.id }</p></td>
<td><p>${order.total }</p></td>
<td>
<c:forEach items="${order.itemList }" var="item">
<p>${item.goodsName }(${item.price }) x ${item.amount }</p>
</c:forEach>
</td>
<td>
<p>${order.name }</p>
<p>${order.phone }</p>
<p>${order.address }</p>
</td>
<td>
<p>
<c:if test="${order.status==2 }"><span style="color:red;">已付款</span></c:if>
<c:if test="${order.status==3 }"><span style="color:green;">已发货</span></c:if>
<c:if test="${order.status==4 }"><span style="color:black;">已完成</span></c:if>
</p>
</td>
<td>
<p>
<c:if test="${order.paytype==1 }">微信</c:if>
<c:if test="${order.paytype==2 }">支付宝</c:if>
<c:if test="${order.paytype==3 }">货到付款</c:if>
</p>
</td>
<td><p>${order.datetime }</p></td>
</tr>
</c:forEach>
</table>
</div>
</div>
<!--//cart-items-->
<!--footer-->
<jsp:include page="footer.jsp"></jsp:include>
<!--//footer-->
</body>
</html>
admin/order_list
<%@ page contentType="text/html;charset=UTF-8"
language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<title>订单列表</title>
<link rel="stylesheet" href="css/bootstrap.css"/>
</head>
<body>
<div class="container-fluid">
<jsp:include page="header.jsp"></jsp:include>
<br>
<ul role="tablist" class="nav nav-tabs">
<li <c:if test="${status==0 }">class="active"</c:if> role="presentation"><a href="/admin/order_list">全部订单</a></li>
<li <c:if test="${status==1 }">class="active"</c:if> role="presentation"><a href="/admin/order_list?status=1">未付款</a></li>
<li <c:if test="${status==2 }">class="active"</c:if> role="presentation"><a href="/admin/order_list?status=2">已付款</a></li>
<li <c:if test="${status==3 }">class="active"</c:if> role="presentation"><a href="/admin/order_list?status=3">配送中</a></li>
<li <c:if test="${status==4 }">class="active"</c:if> role="presentation"><a href="/admin/order_list?status=4">已完成</a></li>
</ul>
<br>
<table class="table table-bordered table-hover">
<tr>
<th width="5%">ID</th>
<th width="5%">总价</th>
<th width="15%">商品详情</th>
<th width="20%">收货信息</th>
<th width="10%">订单状态</th>
<th width="10%">支付方式</th>
<th width="10%">下单用户</th>
<th width="10%">下单时间</th>
<th width="10%">操作</th>
</tr>
<c:forEach items="${p.list }" var="order">
<tr>
<td><p>${order.id }</p></td>
<td><p>${order.total }</p></td>
<td>
<c:forEach items="${order.itemList }" var="item">
<p>${item.goodsName }(${item.price }) x ${item.amount}</p>
</c:forEach>
</td>
<td>
<p>${order.name }</p>
<p>${order.phone }</p>
<p>${order.address }</p>
</td>
<td>
<p>
<c:if test="${order.status==2 }"><span style="color:red;">已付款</span></c:if>
<c:if test="${order.status==3 }"><span style="color:green;">已发货</span></c:if>
<c:if test="${order.status==4 }"><span style="color:black;">已完成</span></c:if>
</p>
</td>
<td>
<p>
<c:if test="${order.paytype==1 }">微信</c:if>
<c:if test="${order.paytype==2 }">支付宝</c:if>
<c:if test="${order.paytype==3 }">货到付款</c:if>
</p>
</td>
<td><p>${order.user.username }</p></td>
<td><p>${order.datetime }</p></td>
<td>
<c:if test="${order.status==2 }">
<a class="btn btn-success" href="/admin/order_status?id=${order.id }&status=3">发货</a>
</c:if>
<c:if test="${order.status==3 }">
<a class="btn btn-warning" href="/admin/order_status?id=${order.id }&status=4">完成</a>
</c:if>
<a class="btn btn-danger" href="/admin/order_delete?id=${order.id }&pageNumber=${p.pageNumber}&status=${status}">删除</a>
</td>
</tr>
</c:forEach>
</table>
<br>
<jsp:include page="/page.jsp">
<jsp:param value="/admin/order_list" name="url"/>
<jsp:param value="&status=${status}" name="param"/>
</jsp:include>
<br>
</div>
</body>
</html>
三、请简要描述过滤器拦截登录的核心代码,以及过滤器类的配置文件。
AdminFilter.class类拦截用户直接通过输入url进入管理员页面,如果通过url访问admin/…会进行拦截,判断是否有用户,如果没有进入index.jsp页面,否则将请求转发给过滤器链上下一个对象。
import model.User;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebFilter(filterName = "AdminFilter",urlPatterns = "/admin/*")
public class AdminFilter implements Filter {
public void destroy() {
}
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
HttpServletRequest request = (HttpServletRequest)req;
HttpServletResponse requestp = (HttpServletResponse)resp;
User u = (User) request.getSession().getAttribute("user");
if(u==null || u.isIsadmin()==false) {
requestp.sendRedirect("../index.jsp");
}else {
// pass the request along the filter chain
chain.doFilter(req, resp);
}
}
public void init(FilterConfig config) throws ServletException {
}
}
EncodeFilter.class过滤器实现对所有的页面内容经行utf-8编码;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
@WebFilter(filterName = "EncodeFilter",urlPatterns = "/*")
public class EncodeFilter implements Filter {
public void destroy() {
}
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
chain.doFilter(req, resp);
}
public void init(FilterConfig config) throws ServletException {
}
}
四、请简要该系统中使用MVC模式分别如何封装,用到了什么框架,并举例说明一下你是如何封装查询商品列表功能的?
module包中的类是对数据库表的属性进行封装,和其他页面需要展示的数据类型封装。 |
dao包中的类是对数据库的操作的语句,每一条语句对应一个方法,然后把结果返回到servlet包中的类。 |
Service包中的类是将查询到的内容以对象的方式保存在request作用域中,然后在转发进入的页面。 |
Servlet包中的类是sql语句对应的方法,类似于mybatis中的接口,主要负责对实现sql语句后的数据进行保存特殊的形式,如list,实例化对象等。 |
Web包向用户页面展示页面。 |
本项目使用MVC的思想设计架构,使用Servlet获取和发送请求,Filter拦截请求,通过jsp进行页面展示。
举例:如图获取全部系列的蛋糕具体步骤如下
首先通过首页index.jsp(实际为header.jsp的内容,只是被引用了)目录单击商品分类,出现下拉菜单栏,再点击全部系列,会跳转到GoodsListServlet类中,(这里只举内容,不考虑客户登录和分页)GoodsListServlet类通过调用GoodsService类中的selectPageByTypeId方法,selectPageByTypeId方法再去调用GoodsDao中的getCountOfGoodsByTypeID去获取数据库的内容,内容以对象的方式保存在request作用域中,然后在转发进入的页面,通过goods_list页面展示数据。文章来源:https://www.toymoban.com/news/detail-767185.html
到了这里,关于基于java,jsp,mysql开发蛋糕商城的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!