|
|
|
@ -1,312 +0,0 @@
|
|
|
|
|
package com.itbaizhan.action;
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.sql.ResultSet;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Date;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
import javax.servlet.RequestDispatcher;
|
|
|
|
|
import javax.servlet.ServletConfig;
|
|
|
|
|
import javax.servlet.ServletException;
|
|
|
|
|
import javax.servlet.http.HttpServlet;
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
|
|
|
|
|
import com.itbaizhan.dao.DB;
|
|
|
|
|
import com.itbaizhan.orm.Tgoods;
|
|
|
|
|
import com.itbaizhan.service.liuService;
|
|
|
|
|
|
|
|
|
|
public class goods_servlet extends HttpServlet {
|
|
|
|
|
// 处理所有的请求,根据type来区分不同的操作
|
|
|
|
|
public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
|
|
|
|
|
String type = req.getParameter("type");
|
|
|
|
|
|
|
|
|
|
// 判断type并调用相应的方法进行处理
|
|
|
|
|
if (type.endsWith("goodsAdd")) {
|
|
|
|
|
goodsAdd(req, res); // 添加商品
|
|
|
|
|
}
|
|
|
|
|
if (type.endsWith("goodsMana")) {
|
|
|
|
|
goodsMana(req, res); // 商品管理
|
|
|
|
|
}
|
|
|
|
|
if (type.endsWith("goodsDel")) {
|
|
|
|
|
goodsDel(req, res); // 删除商品
|
|
|
|
|
}
|
|
|
|
|
if (type.endsWith("goodsDetailHou")) {
|
|
|
|
|
goodsDetailHou(req, res); // 后台查看商品详细信息
|
|
|
|
|
}
|
|
|
|
|
if (type.endsWith("goodsPre")) {
|
|
|
|
|
goodsPre(req, res); // 商品信息添加前的准备工作
|
|
|
|
|
}
|
|
|
|
|
if (type.endsWith("goodsEdit")) {
|
|
|
|
|
goodsEdit(req, res); // 编辑商品信息
|
|
|
|
|
}
|
|
|
|
|
if (type.endsWith("goodsNew")) {
|
|
|
|
|
goodsNew(req, res); // 获取最新商品
|
|
|
|
|
}
|
|
|
|
|
if (type.endsWith("goodsByCatelog")) {
|
|
|
|
|
goodsByCatelog(req, res); // 按商品类别查看商品
|
|
|
|
|
}
|
|
|
|
|
if (type.endsWith("goodsDetailQian")) {
|
|
|
|
|
goodsDetailQian(req, res); // 前台查看商品详细信息
|
|
|
|
|
}
|
|
|
|
|
if (type.endsWith("goodsRes")) {
|
|
|
|
|
goodsRes(req, res); // 根据商品名称搜索商品
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 添加商品
|
|
|
|
|
public void goodsAdd(HttpServletRequest req, HttpServletResponse res) {
|
|
|
|
|
String id = String.valueOf(new Date().getTime()); // 使用当前时间戳作为商品 ID
|
|
|
|
|
String catelog_id = req.getParameter("catelog_id");
|
|
|
|
|
String bianhao = req.getParameter("bianhao");
|
|
|
|
|
String mingcheng = req.getParameter("mingcheng");
|
|
|
|
|
String jieshao = req.getParameter("jieshao");
|
|
|
|
|
String fujian = req.getParameter("fujian");
|
|
|
|
|
int shichangjia = Integer.parseInt(req.getParameter("shichangjia"));
|
|
|
|
|
int tejia = Integer.parseInt(req.getParameter("shichangjia")); // 可能是设置的特价,暂时设置为与市场价一样
|
|
|
|
|
|
|
|
|
|
String del = "no"; // 标记商品未删除
|
|
|
|
|
|
|
|
|
|
// SQL语句插入商品数据
|
|
|
|
|
String sql = "insert into t_goods(id, catelog_id, bianhao, mingcheng, jieshao, fujian, shichangjia, tejia, del) values(?,?,?,?,?,?,?,?,?)";
|
|
|
|
|
Object[] params = {id, catelog_id, bianhao, mingcheng, jieshao, fujian, shichangjia, tejia, del};
|
|
|
|
|
|
|
|
|
|
// 使用数据库操作类执行插入操作
|
|
|
|
|
DB mydb = new DB();
|
|
|
|
|
mydb.doPstm(sql, params); // 执行SQL插入
|
|
|
|
|
mydb.closed(); // 关闭数据库连接
|
|
|
|
|
|
|
|
|
|
// 返回操作成功信息并跳转至指定页面
|
|
|
|
|
req.setAttribute("msg", "操作成功");
|
|
|
|
|
String targetURL = "/common/msg.jsp";
|
|
|
|
|
dispatch(targetURL, req, res); // 转发到操作成功页面
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 商品管理,查看所有商品
|
|
|
|
|
public void goodsMana(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
|
|
|
|
|
List goodsList = new ArrayList();
|
|
|
|
|
String sql = "select * from t_goods where del='no'"; // 查询所有未删除的商品
|
|
|
|
|
Object[] params = {};
|
|
|
|
|
|
|
|
|
|
DB mydb = new DB();
|
|
|
|
|
try {
|
|
|
|
|
mydb.doPstm(sql, params); // 执行查询操作
|
|
|
|
|
ResultSet rs = mydb.getRs();
|
|
|
|
|
while (rs.next()) {
|
|
|
|
|
Tgoods goods = new Tgoods(); // 创建Tgoods对象并填充数据
|
|
|
|
|
goods.setId(rs.getString("id"));
|
|
|
|
|
goods.setCatelog_id(rs.getString("catelog_id"));
|
|
|
|
|
goods.setBianhao(rs.getString("bianhao"));
|
|
|
|
|
goods.setMingcheng(rs.getString("mingcheng"));
|
|
|
|
|
goods.setJieshao(rs.getString("jieshao"));
|
|
|
|
|
goods.setFujian(rs.getString("fujian"));
|
|
|
|
|
goods.setShichangjia(rs.getInt("shichangjia"));
|
|
|
|
|
goods.setTejia(rs.getInt("tejia"));
|
|
|
|
|
goods.setDel(rs.getString("del"));
|
|
|
|
|
goodsList.add(goods); // 将商品添加到列表中
|
|
|
|
|
}
|
|
|
|
|
rs.close();
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
mydb.closed(); // 关闭数据库连接
|
|
|
|
|
|
|
|
|
|
// 将商品列表传递到前端页面
|
|
|
|
|
req.setAttribute("goodsList", goodsList);
|
|
|
|
|
req.getRequestDispatcher("admin/goods/goodsMana.jsp").forward(req, res);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 删除商品
|
|
|
|
|
public void goodsDel(HttpServletRequest req, HttpServletResponse res) {
|
|
|
|
|
String id = req.getParameter("id");
|
|
|
|
|
String sql = "update t_goods set del='yes' where id=" + id; // 更新商品的del字段为'yes'表示删除
|
|
|
|
|
Object[] params = {};
|
|
|
|
|
|
|
|
|
|
DB mydb = new DB();
|
|
|
|
|
mydb.doPstm(sql, params); // 执行删除操作
|
|
|
|
|
mydb.closed();
|
|
|
|
|
|
|
|
|
|
req.setAttribute("msg", "操作成功");
|
|
|
|
|
String targetURL = "/common/msg.jsp";
|
|
|
|
|
dispatch(targetURL, req, res); // 转发到操作成功页面
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 后台查看商品详细信息
|
|
|
|
|
public void goodsDetailHou(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
|
|
|
|
|
String id = req.getParameter("id");
|
|
|
|
|
|
|
|
|
|
req.setAttribute("goods", liuService.getGoods(id)); // 从服务层获取商品详细信息
|
|
|
|
|
req.getRequestDispatcher("admin/goods/goodsDetailHou.jsp").forward(req, res);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 商品信息添加前的准备工作
|
|
|
|
|
public void goodsPre(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
|
|
|
|
|
Tgoods goods = new Tgoods();
|
|
|
|
|
String sql = "select * from t_goods where id=?";
|
|
|
|
|
Object[] params = {req.getParameter("id")};
|
|
|
|
|
|
|
|
|
|
DB mydb = new DB();
|
|
|
|
|
try {
|
|
|
|
|
mydb.doPstm(sql, params); // 执行查询操作
|
|
|
|
|
ResultSet rs = mydb.getRs();
|
|
|
|
|
while (rs.next()) {
|
|
|
|
|
goods.setId(rs.getString("id"));
|
|
|
|
|
goods.setCatelog_id(rs.getString("catelog_id"));
|
|
|
|
|
goods.setBianhao(rs.getString("bianhao"));
|
|
|
|
|
goods.setMingcheng(rs.getString("mingcheng"));
|
|
|
|
|
goods.setJieshao(rs.getString("jieshao"));
|
|
|
|
|
goods.setFujian(rs.getString("fujian"));
|
|
|
|
|
goods.setShichangjia(rs.getInt("shichangjia"));
|
|
|
|
|
goods.setTejia(rs.getInt("tejia"));
|
|
|
|
|
goods.setDel(rs.getString("del"));
|
|
|
|
|
}
|
|
|
|
|
rs.close();
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
mydb.closed();
|
|
|
|
|
|
|
|
|
|
req.setAttribute("goods", goods); // 将商品信息传递到前端页面
|
|
|
|
|
req.getRequestDispatcher("admin/goods/goodsPre.jsp").forward(req, res);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 编辑商品信息
|
|
|
|
|
public void goodsEdit(HttpServletRequest req, HttpServletResponse res) {
|
|
|
|
|
String id = req.getParameter("id");
|
|
|
|
|
String catelog_id = req.getParameter("catelog_id");
|
|
|
|
|
String bianhao = req.getParameter("bianhao");
|
|
|
|
|
String mingcheng = req.getParameter("mingcheng");
|
|
|
|
|
String jieshao = req.getParameter("jieshao");
|
|
|
|
|
String fujian = req.getParameter("fujian");
|
|
|
|
|
int shichangjia = Integer.parseInt(req.getParameter("shichangjia"));
|
|
|
|
|
int tejia = Integer.parseInt(req.getParameter("shichangjia"));
|
|
|
|
|
|
|
|
|
|
// 更新商品信息
|
|
|
|
|
String sql = "update t_goods set catelog_id=?, bianhao=?, mingcheng=?, jieshao=?, fujian=?, shichangjia=?, tejia=? where id=?";
|
|
|
|
|
Object[] params = {catelog_id, bianhao, mingcheng, jieshao, fujian, shichangjia, tejia, id};
|
|
|
|
|
|
|
|
|
|
DB mydb = new DB();
|
|
|
|
|
mydb.doPstm(sql, params); // 执行更新操作
|
|
|
|
|
mydb.closed();
|
|
|
|
|
|
|
|
|
|
req.setAttribute("msg", "操作成功");
|
|
|
|
|
String targetURL = "/common/msg.jsp";
|
|
|
|
|
dispatch(targetURL, req, res); // 转发到操作成功页面
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取最新商品
|
|
|
|
|
public void goodsNew(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
|
|
|
|
|
List goodsList = new ArrayList();
|
|
|
|
|
String sql = "select * from t_goods where del='no' order by id desc"; // 按商品ID降序排列,查询所有未删除的商品
|
|
|
|
|
Object[] params = {};
|
|
|
|
|
|
|
|
|
|
DB mydb = new DB();
|
|
|
|
|
try {
|
|
|
|
|
mydb.doPstm(sql, params); // 执行查询操作
|
|
|
|
|
ResultSet rs = mydb.getRs();
|
|
|
|
|
while (rs.next()) {
|
|
|
|
|
Tgoods goods = new Tgoods(); // 创建Tgoods对象并填充数据
|
|
|
|
|
goods.setId(rs.getString("id"));
|
|
|
|
|
goods.setCatelog_id(rs.getString("catelog_id"));
|
|
|
|
|
goods.setBianhao(rs.getString("bianhao"));
|
|
|
|
|
goods.setMingcheng(rs.getString("mingcheng"));
|
|
|
|
|
goods.setJieshao(rs.getString("jieshao"));
|
|
|
|
|
goods.setFujian(rs.getString("fujian"));
|
|
|
|
|
goods.setShichangjia(rs.getInt("shichangjia"));
|
|
|
|
|
goods.setTejia(rs.getInt("tejia"));
|
|
|
|
|
goods.setDel(rs.getString("del"));
|
|
|
|
|
goodsList.add(goods); // 将商品添加到列表中
|
|
|
|
|
}
|
|
|
|
|
rs.close();
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
mydb.closed(); // 关闭数据库连接
|
|
|
|
|
|
|
|
|
|
// 限制最新商品显示数量最多为4个
|
|
|
|
|
if (goodsList.size() > 4) {
|
|
|
|
|
goodsList = goodsList.subList(0, 4); // 截取前4个商品
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 将商品列表传递到前端页面
|
|
|
|
|
req.setAttribute("goodsList", goodsList);
|
|
|
|
|
req.getRequestDispatcher("site/goods/goodsNew.jsp").forward(req, res); // 转发到前端页面显示最新商品
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 根据商品类别查看商品
|
|
|
|
|
public void goodsByCatelog(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
|
|
|
|
|
String catelog_id = req.getParameter("catelog_id");
|
|
|
|
|
|
|
|
|
|
// 从服务层获取指定类别的商品列表
|
|
|
|
|
req.setAttribute("goodsList", liuService.goodsByCatelog(catelog_id));
|
|
|
|
|
req.getRequestDispatcher("site/goods/goodsByCatelog.jsp").forward(req, res); // 转发到前端页面显示商品类别
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 前台查看商品详细信息
|
|
|
|
|
public void goodsDetailQian(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
|
|
|
|
|
String id = req.getParameter("id");
|
|
|
|
|
|
|
|
|
|
// 从服务层获取商品的详细信息
|
|
|
|
|
req.setAttribute("goods", liuService.getGoods(id));
|
|
|
|
|
req.getRequestDispatcher("site/goods/goodsDetailQian.jsp").forward(req, res); // 转发到前端页面显示商品详细信息
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 根据商品名称搜索商品
|
|
|
|
|
public void goodsRes(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
|
|
|
|
|
String mingcheng = req.getParameter("mingcheng");
|
|
|
|
|
|
|
|
|
|
List goodsList = new ArrayList();
|
|
|
|
|
String sql = "select * from t_goods where del='no' and mingcheng like '%" + mingcheng.trim() + "%'"; // 根据商品名称进行模糊查询
|
|
|
|
|
Object[] params = {};
|
|
|
|
|
|
|
|
|
|
DB mydb = new DB();
|
|
|
|
|
try {
|
|
|
|
|
mydb.doPstm(sql, params); // 执行查询操作
|
|
|
|
|
ResultSet rs = mydb.getRs();
|
|
|
|
|
while (rs.next()) {
|
|
|
|
|
Tgoods goods = new Tgoods(); // 创建Tgoods对象并填充数据
|
|
|
|
|
goods.setId(rs.getString("id"));
|
|
|
|
|
goods.setCatelog_id(rs.getString("catelog_id"));
|
|
|
|
|
goods.setBianhao(rs.getString("bianhao"));
|
|
|
|
|
goods.setMingcheng(rs.getString("mingcheng"));
|
|
|
|
|
goods.setJieshao(rs.getString("jieshao"));
|
|
|
|
|
goods.setFujian(rs.getString("fujian"));
|
|
|
|
|
goods.setShichangjia(rs.getInt("shichangjia"));
|
|
|
|
|
goods.setTejia(rs.getInt("tejia"));
|
|
|
|
|
goods.setDel(rs.getString("del"));
|
|
|
|
|
goodsList.add(goods); // 将商品添加到列表中
|
|
|
|
|
}
|
|
|
|
|
rs.close();
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
mydb.closed(); // 关闭数据库连接
|
|
|
|
|
|
|
|
|
|
// 将商品列表传递到前端页面
|
|
|
|
|
req.setAttribute("goodsList", goodsList);
|
|
|
|
|
req.getRequestDispatcher("site/goods/goodsRes.jsp").forward(req, res); // 转发到前端页面显示搜索结果
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 转发请求到指定页面
|
|
|
|
|
public void dispatch(String targetURI, HttpServletRequest request, HttpServletResponse response) {
|
|
|
|
|
RequestDispatcher dispatch = getServletContext().getRequestDispatcher(targetURI);
|
|
|
|
|
try {
|
|
|
|
|
dispatch.forward(request, response); // 转发请求到目标页面
|
|
|
|
|
} catch (ServletException e) {
|
|
|
|
|
e.printStackTrace(); // 捕获并打印异常
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace(); // 捕获并打印异常
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 初始化方法,初始化Servlet时调用
|
|
|
|
|
public void init(ServletConfig config) throws ServletException {
|
|
|
|
|
super.init(config); // 调用父类的init()方法进行初始化
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 销毁方法,在Servlet销毁时调用
|
|
|
|
|
public void destroy() {
|
|
|
|
|
// 在Servlet销毁时执行清理工作,如果有的话
|
|
|
|
|
}
|
|
|
|
|
}
|