目录结构
index.jsp 书城首页 遍历书籍 web界面
<c:forEach items="${page.list }" var="menu">
<div style="margin-top: 10px; margin-left: 30px; float: left; width: 300px" >
<table >
<tr >
<td rowspan="5" class="bookPic"><img
src="${menu.imgpath}" width="150px" height="140px" style="border-radius:10px"
/></td>
<td><span>书名:</span></td>
<td><span><strong>${menu.name}</strong></span></td>
</tr>
<tr>
<td><span>市场价格:</span></td>
<td><span>${menu.price}</span></td>
</tr>
<tr>
<td><span>会员价格:</span></td>
<td><span><strong style="color: red;">${menu.price1}</strong></span></td>
</tr>
<tr>
<td><span>标签:</span></td>
<td><span>${menu.burden}</span></td>
</tr>
<tr>
<td><span>书籍类型:</span></td>
<td><span>${menu.typename}</span></td>
</tr>
<tr>
<td colspan="2" style="height: 40px;"><a href="${pageContext.request.contextPath}/shoppingCartServlet?action=add&&menuid=${menu.id}&&curPage=${page.curPage}"><img src=""><img src="${pageContext.request.contextPath}/qiantai/images/3.jpg" width=50 height=28 title="加入购物车"><strong>购物车</strong></a></td>
<td></td>
</tr>
</table>
</div>
</c:forEach>
MenusServlet.java 查询所有书籍
private void findMenusAll(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String curPage=request.getParameter("curPage");
int curpagestr=0;
if(curPage==null||"".equals(curPage)){
curpagestr=1;
}else{
curpagestr=Integer.parseInt(curPage);
}
Page page=new Page();
page.setCurPage(curpagestr);
page=menusService.getMenusAll(page);
request.setAttribute("page", page);
request.getRequestDispatcher("/admin/menus.jsp").forward(request, response);
}
MenusDaoImpl.java 数据库查询
@Override
public List<Menus> getMenusAll() {
String sql="select menus.id, menus.name,imgpath,burden,types.name as typename,brief,price1,sums1,price,sums from menus inner JOIN types on types.id=menus.typeid ORDER BY sums1 desc LIMIT 0,2 ";
List list=null;
try {
list=dbutil.getQueryList(Menus.class, sql, null);
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
index.jsp 公告遍历:
<c:forEach items="${requestScope.notice }" var="notice" begin="0" end="1">
<ul class="newslist_time2">
<li class="newslist_time2">
<div class="time">${notice.times.substring(0,10) }</div>
<a href="${pageContext.request.contextPath }/indexServlet?action=findNotice&&id=${notice.id }" class="newslist_time2">${notice.name }</a>
</li>
</ul>
</c:forEach>
NoticeServlet.java 查询公告
private void findAll(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
List<Notice> list=noticeService.getNoticeAll();
request.setAttribute("notices",list);
request.getRequestDispatcher("/admin/notice.jsp").forward(request, response);
}
删除公告
private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
PrintWriter out=response.getWriter();
int id=Integer.parseInt(request.getParameter("id"));
Notice notice=new Notice();
notice.setId(id);
int result=noticeService.deleteNotice(id);
if(result==1){
out.write("");
}else if(result==0){
out.write("");
}
}
添加公告
private void add(HttpServletRequest request, HttpServletResponse response) throws IOException {
PrintWriter out=response.getWriter();
String name=request.getParameter("name");
String content=request.getParameter("content");
System.out.println(name);
Notice notice=new Notice();
notice.setName(name);
notice.setContent(content);
SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time=df.format(new Date());
notice.setTimes(time);
int result=noticeService.addNotice(notice);
if(result==1){
out.write("");
}else if(result==-1){
out.write("");
}else{
out.write("");
}
}
index.jsp 销售量遍历
<c:forEach items="${sessionScope.shoppingCart }" var="cart">
<tr>
<td align="center">${cart.name}</td>
<td align="center">${cart.price}</td>
<td align="center">${cart.sum}</td>
<td align="center"><a
href="${pageContext.request.contextPath}/shoppingCartServlet?action=del&&menuid=${cart.menuid}&&curPage=${page.curPage}">取消</a></td>
<c:set var="count" value="${count+cart.sum }"></c:set>
<c:set var="price1" value="${price1+cart.sum*cart.price }"></c:set>
</tr>
</c:forEach>
OrderDaoImpl.java 数据库查询订单
@Override
public Page getOrderAll(Page page) {
String sql="select menus.id menuid ,menus.name menuname,SUM(menusum) sum,menus.price price from menus inner join orders on menus.id=orders.menuid group by menuid order by sum desc ";
Page page1=null;
page1 = dbutil.getQueryPage(OrderStatistics.class, sql, null, page);
return page1;
}
Shoppingcar.jsp 购物车web界面
<tr>
<td class="line_table" align="center" width="40%"><span
class="left_bt2">菜品名称</span></td>
<td class="line_table" align="center" width="20%"><span
class="left_bt2">单价</span></td>
<td class="line_table" align="center" width="20%"><span
class="left_bt2">数量</span></td>
<td class="line_table" align="center" width="20%"><span
class="left_bt2"> </span></td>
</tr>
<c:forEach items="${sessionScope.shoppingCart}" var="cart">
<tr>
<td class="line_table" align="center" width="40%"><span
class="left_txt">${cart.name}</span></td>
<td class="line_table" align="center" width="20%"><span
class="left_txt">${cart.price}</span></td>
<td class="line_table" align="center" width="20%"><span
class="left_txt">${cart.sum}</span></td>
<td class="line_table" align="center" width="20%"><a
href="${pageContext.request.contextPath }/shoppingCartServlet?action=del&&menuid=${cart.menuid}">取消</a></td>
<c:set var="count" value="${count+cart.sum}"> </c:set>
<c:set var="price1" value="${price1+cart.sum*cart.price}"></c:set>
</tr>
</c:forEach>
ShoppingCarServlet.java 购物车
添加购物车
private void addCart(HttpServletRequest request,
HttpServletResponse response) throws IOException {
int menuid=Integer.parseInt(request.getParameter("menuid"));
HttpSession session=request.getSession();
ArrayList<ShoppingCart> cartList=(ArrayList<ShoppingCart>) session.getAttribute("shoppingCart");
boolean flag=false;
if(cartList==null){
cartList=new ArrayList<ShoppingCart>();
}else{
for(ShoppingCart cart:cartList){
if(cart.getMenuid()==menuid){
cart.setSum(cart.getSum()+1);
flag=true;
break;
}
}
}
if(!flag)
{
ShoppingCart cart=new ShoppingCart();
Menus menus=service.getMenusByID(menuid);
cart.setMenuid(menuid);
cart.setName(menus.getName());
cart.setPrice(Float.parseFloat(menus.getPrice()));
cart.setSum(1);
cartList.add(cart);
}
session.setAttribute("shoppingCart", cartList);
response.sendRedirect(request.getContextPath()+"/qiantai/index.jsp");
}
在该页面中还有查询购物车,清空购物车
order.jsp 前台订单界面
<c:forEach items="${requestScope.page.list }" var="order">
<tr height=30>
<td class="line_table" align="center"><span
class="left_txt"><strong>${order.menuname }</span></td>
<td class="line_table" align="center"><span
class="left_txt"><strong>${order.realname }</span></td>
<td class="line_table" align="center"><span
class="left_txt"><strong>${order.phone }</span></td>
<td class="line_table" align="center"><span
class="left_txt"><strong>${order.address }</span></td>
<td class="line_table" align="center"><span
class="left_txt"><strong>${order.menusum}</span></td>
<td class="line_table" align="center"><span
class="left_txt"><strong>${order.price}</span></td>
<td class="line_table" align="center"><span
class="left_txt"><strong><font color=red>${order.price*order.menusum }</span></td>
<td class="line_table" align="center"><span
class="left_txt"><strong>${order.times}</span></td>
<td class="line_table" align="center"><span
class="left_txt"><strong>${order.delivery}</strong></span></td>
</tr>
<tr><th colspan=9 height=2 bgcolor="#b8b2a6"></th></tr>
</c:forEach>
OrderServlet.java 前台订单
生成订单
private void addOrder(HttpServletRequest request,
HttpServletResponse response) throws IOException {
PrintWriter out=response.getWriter();
HttpSession session=request.getSession();
User user=(User) session.getAttribute("user");
if(user!=null){
ArrayList<ShoppingCart> cartList=(ArrayList<ShoppingCart>) session.getAttribute("shoppingCart");
boolean result;
try {
result=orderService.addOrders(cartList, user.getId());
if(result){
session.removeAttribute("shoppingCart");
out.write("");
}else{
out.write("");
}
} catch (Exception e) {
e.printStackTrace();
}
}else{
out.write("");
}
}
}
插入订单数据库
public int addOrders(Orders orders) {
String sql="insert into orders(userid,menuid,menusum,times,delivery) values(?,?,?,?,?)";
Object[] obj={orders.getId(),orders.getMenuid(),orders.getMenusum(),orders.getTimes(),orders.getDelivery()};
int result=0;
try {
result=dbutil.execute(DBUtil.getConnection(), sql, obj);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
查询该用户订单
private void findOrdersAll(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String curPage=request.getParameter("curPage");
int curpagestr=0;
if(curPage==null||"".equals(curPage)){
curpagestr=1;
}else{
curpagestr=Integer.parseInt(curPage);
}
Page1 page=new Page1();
page.setCurPage(curpagestr);
page=orderService.getOrderAll1(page);
request.setAttribute("page", page);
request.getRequestDispatcher("/admin/order.jsp").forward(request, response);
}
数据库操作:根据用户名,用户ID,书籍名称
public Page getOrderByID(Page page, String userid) {
String sql="select userid,users.realname,users.phone,users.address,menus.name menuname,orders.menusum,menus.price,orders.times,delivery from orders inner join users on orders.userid=users.id inner join menus on orders.menuid=menus.id where users.name=? order by orders.times desc";
Page page1=null;
Object[] obj={userid};
page1=dbutil.getQueryPage(Orders.class, sql, obj, page);
return page1;
}
@Override
public Page getOrderByMenuName(Page page, String userid, String menuname) {
String sql="select users.realname,users.phone,users.address,menus.name menuname,orders.menusum,menus.price,orders.times,delivery from orders inner join users on orders.userid=users.id inner join menus on orders.menuid=menus.id where users.name=? and menus.name like '%"+menuname+"%'";
Page page1=null;
Object[] obj={userid};
page1=dbutil.getQueryPage(Orders.class, sql, obj, page);
return page1;
}
@Override
public Page getadminOrderByMenuName(Page page, String menuname) {
String sql="select users.realname,users.phone,users.address,menus.name menuname,orders.menusum,menus.price,orders.times,delivery from orders inner join users on orders.userid=users.id inner join menus on orders.menuid=menus.id where menus.name like '%"+menuname+"%'";
Page page1=null;
page1=dbutil.getQueryPage(Orders.class, sql, null, page);
return page1;
}
其余前端界面代码不做赘述
下面开始介绍后台代码
书籍列表
<c:forEach items="${requestScope.page.list }" var="menus">
<tr onmouseover="this.style.backgroundColor='#c5c5c5';" onmouseout="this.style.backgroundColor='#ececec';">
<td class="line_table" align="center"><a
href="menus_update.jsp?">${menus.name}</a></td>
<td class="line_table" align="center" height=55 width=65><a
href="${pageContext.request.contextPath }/${menus.imgpath}"><img src="${pageContext.request.contextPath }/${menus.imgpath}"
width="60" height="50"></a></td>
<td class="line_table" align="center"><span
class="left_txt">${menus.burden}</span></td>
<td class="line_table" align="center"><span
class="left_txt">${menus.typename}</span></td>
<td class="line_table" align="center"><span
class="left_txt">${menus.brief}</span></td>
<td class="line_table" align="center"><span
class="left_txt">${menus.price}</span></td>
<td class="line_table" align="center"><span
class="left_txt">${menus.sums}</span></td>
<td class="line_table" align="center"><span
class="left_txt">${menus.price1}</span></td>
<td class="line_table" align="center"><span
class="left_txt">${menus.sums1}</span></td>
<td class="line_table" align="center"><a
href="${pageContext.request.contextPath}/menusServlet?action=findID&&id=${menus.id}"><img src="${pageContext.request.contextPath}/admin/images/update1.png" width=80 height=35 style="border-radius:5px" title="修改"></a></td>
<td class="line_table" align="center"><a
href="${pageContext.request.contextPath}/menusServlet?action=delete&&name=${menus.name}"><img src="${pageContext.request.contextPath}/admin/images/delete.png" width=80 height=33 style="border-radius:5px" title="删除"></a></td>
</tr>
<tr><th colspan=11 height=8></th></tr>
</c:forEach>
根据书籍名称。用户名称查询 order_search.jsp
<form action="${pageContext.request.contextPath}/ordersServlet?action=orderSearch4&&curPage=1" name="form1"
method="post">
<tr>
<td class="line_table" align="right" width="40%"><span
class="left_bt2">按用户姓名查询</span></td>
<td class="line_table" align="left" width="60%"><input
type="text" name="username" size="20">
</td><tr>
<td class="line_table" align="right" width="40%"><span
class="left_bt2">按菜品名称查询</span></td>
<td class="line_table" align="left" width="60%"><input
type="text" name="menuname" size="20">
<input
type="submit" value="." style="background-image:url(${pageContext.request.contextPath}/admin/images/find.png); background-size:100%;opacity:0.8;background-color:#5ca0d3;height:25px;width:70px;font-size:20px;border:none;border-radius:10px"></td>
</table>
</form>
其余功能较为相似,不做赘述