<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="java.util.List,com.zl.entity.*" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>商品操作页面</title>
</head>
<body>
<c:choose>
<c:when test="${ empty userinfo}">
<jsp:forward page="login.html"></jsp:forward>
</c:when>
<c:otherwise>
<c:out value="欢迎${userinfo.name}来到系统"></c:out>
<center>
<br/><br/><br/>
<table width=70% border=1 cellspacing=0 cellpadding=0 style='text-align:center'>
<caption>商品列表</caption>
<tr>
<th>商品编号</th>
<th>商品名称</th>
<th>商品价格</th>
<th>商品描述</th>
<th>生产日期</th>
<th>商品类型</th>
<th>操作</th>
</tr>
<c:forEach items="${goodlist}" var="goodlist">
<tr>
<td>${goodlist.gid}</td>
<td>${goodlist.gname}</td>
<td>${goodlist.price}</td>
<td>${goodlist.gdesc}</td>
<td>${goodlist.createDate}</td>
<td>${goodlist.typename}</td>
<td><a href="goodsCRUD?method=showUpdate&gid=${goodlist.gid}">修改</a> | <a href="goodsCRUD?method=del&gid=${goodlist.gid}">删除</a></td>
</tr>
</c:forEach>
<tr>
<td colspan="7"><a href="goodsCRUD?method=getGoodstype">添加</a></td>
</tr>
</table>
<br/><br/>
<a href="goodsCRUD?method=showSplit&currPage=1">首页</a>
<a href="goodsCRUD?method=showSplit&currPage=${page.currPage-1<=1?1:page.currPage-1}">上一页</a>
<c:forEach begin="1" end="${page.totalPage}" var="i">
<a href="goodsCRUD?method=showSplit&currPage=${i}">${i}</a>
</c:forEach>
<a href="goodsCRUD?method=showSplit&currPage=${page.currPage+1>=page.totalPage?page.totalPage:page.currPage+1}">下一页</a>
<a href="goodsCRUD?method=showSplit&currPage=${page.totalPage}">最后一页</a>
</center>
</c:otherwise>
</c:choose>
</body>
</html>
package com.zl.service;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanUtils;
import com.zl.base.BaseServlet;
import com.zl.dao.GoodCRUDDao;
import com.zl.dao.GoodsTypeDao;
import com.zl.daoimpl.GoodsDaoImpl;
import com.zl.daoimpl.GoodsTypeDaoImpl;
import com.zl.entity.Goods;
import com.zl.entity.Goodstype;
import com.zl.util.Page;
@WebServlet("/goodsCRUD")
public class GoodsService extends BaseServlet {
GoodCRUDDao gd = new GoodsDaoImpl();
GoodsTypeDao dta = new GoodsTypeDaoImpl();
Page page = new Page();
public void showSplit(HttpServletRequest rq, HttpServletResponse rp) throws ServletException, IOException {
String currPage = rq.getParameter("currPage");
if (currPage == null || currPage == "") {
currPage = "1";
}
page.setCurrPage(Integer.parseInt(currPage));
page.setStartNum(Integer.parseInt(currPage));
int n = gd.count();
page.setTotalPage(n);
rq.setAttribute("page", page);
List<Goods> list = gd.selectSplit(page);
if (list.size() > 0) {
rq.setAttribute("goodlist", list);
rq.getRequestDispatcher("WEB-INF/goodsCRUD.jsp").forward(rq, rp);
} else {
rp.getWriter().print("当前没有商品");
}
}
public void del(HttpServletRequest rq, HttpServletResponse rp) throws ServletException, IOException {
int n = gd.del(Integer.parseInt(rq.getParameter("gid")));
if (n > 0) {
showSplit(rq, rp);
} else {
rp.getWriter().print("删除失败!");
rp.sendRedirect("goodsCRUD?method=showSplit");
}
}
public void add(HttpServletRequest rq, HttpServletResponse rp) throws ServletException, IOException {
Goods g = new Goods();
try {
Map<String, String[]> map = rq.getParameterMap();
if (map.isEmpty()) {
rp.getWriter().print("添加失败");
} else {
BeanUtils.populate(g, map);
int n = gd.add(g);
if (n > 0) {
rp.sendRedirect("goodsCRUD?method=showSplit");
}
}
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
public void getGoodstype(HttpServletRequest rq, HttpServletResponse rp) throws ServletException, IOException {
List<Goodstype> typelist = dta.selectAll();
if (typelist != null) {
rq.setAttribute("typelist", typelist);
rq.getRequestDispatcher("WEB-INF/addGoods.jsp").forward(rq, rp);
}
}
public void showUpdate(HttpServletRequest rq, HttpServletResponse rp) throws ServletException, IOException {
int gid = Integer.parseInt(rq.getParameter("gid"));
Goods g = gd.selectById(gid);
if (g != null) {
rq.setAttribute("good", g);
rq.getRequestDispatcher("WEB-INF/goodUpdate.jsp").forward(rq, rp);
}
}
public void update(HttpServletRequest rq, HttpServletResponse rp) throws ServletException, IOException {
Goods g = new Goods();
try {
BeanUtils.populate(g, rq.getParameterMap());
int n = gd.update(g);
rp.sendRedirect("goodsCRUD?method=showSplit");
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
package com.zl.util;
public class Page {
private int currPage;
private static int SIZE = 5;
private int totalPage;
private int startNum;
public Page() {
init();
}
public Page(int currPage, int totalPage, int startNum) {
super();
this.currPage = currPage;
this.totalPage = totalPage;
this.startNum = startNum;
}
public void init() {
if (currPage <= 1) {
currPage = 1;
}
}
public int getCurrPage() {
return currPage;
}
public void setCurrPage(int currPage) {
this.currPage = currPage;
}
public int getSize() {
return SIZE;
}
public void setSize(int size) {
this.SIZE = size;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int n) {
if (n % SIZE == 0) {
this.totalPage = n / SIZE;
} else {
this.totalPage = n / SIZE + 1;
}
}
public int getStartNum() {
return startNum;
}
public void setStartNum(int currPage) {
this.startNum = (currPage - 1) * SIZE;
}
}
package com.zl.daoimpl;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import com.zl.dao.GoodCRUDDao;
import com.zl.entity.Goods;
import com.zl.util.Page;
public class GoodsDaoImpl implements GoodCRUDDao {
@Override
public List<Goods> selectAll() {
String sql = "select * from goods";
List<Goods> list = null;
try {
list = QR.query(sql, new BeanListHandler<Goods>(Goods.class));
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
@Override
public Goods selectById(int id) {
String sql = "select * from goods where gid = ?";
Goods g = null;
try {
g = QR.query(sql, new BeanHandler<Goods>(Goods.class), id);
} catch (SQLException e) {
e.printStackTrace();
}
return g;
}
@Override
public int add(Goods g) {
String sql = "insert into goods(gname,price,gdesc,createDate,typename)values(?,?,?,?,?)";
int n = 0;
try {
n = QR.update(sql, g.getGname(), g.getPrice(), g.getGdesc(), g.getCreateDate(), g.getTypename());
} catch (SQLException e) {
e.printStackTrace();
}
return n;
}
@Override
public int del(int id) {
String sql = "delete from goods where gid=?";
int n = 0;
try {
n = QR.update(sql, id);
} catch (SQLException e) {
e.printStackTrace();
}
return n;
}
@Override
public int update(Goods g) {
String sql = "update goods set gname=?,price=?,gdesc=?,createDate=? where gid=?";
int n = 0;
try {
n = QR.update(sql, g.getGname(), g.getPrice(), g.getGdesc(), g.getCreateDate(), g.getGid());
} catch (SQLException e) {
e.printStackTrace();
}
return n;
}
@Override
public List<Goods> selectSplit(Page page) {
String sql = "select * from goods limit ?,?";
List<Goods> list = null;
try {
list = QR.query(sql, new BeanListHandler<Goods>(Goods.class), page.getStartNum(), page.getSize());
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
public int count() {
String sql = "select count(gid) from goods";
Number n = 0;
try {
n = QR.query(sql, new ScalarHandler<Number>());
} catch (SQLException e) {
e.printStackTrace();
}
return n.intValue();
}
}