基于javaweb+mysql的jsp+servlet超市便利店管理系统(JavaWeb JSP MySQL Servlet SSM SpringBoot Layui Ajax)
私信源码获取及调试交流
运行环境
Java≥8、MySQL≥5.7、Tomcat≥8
开发工具
eclipse/idea/myeclipse/sts等均可配置运行
技术框架
JavaBean MVC JSP Servlet MySQL CSS JavaScript Layui Ajax
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
登录、注册、退出、用户模块、公告模块、商品模块、订单模块、供应商模块的增删改查管理
eclipse/MyEclipse运行:
idea运行:
String to = "get".equalsIgnoreCase(action) ? "info" : "edit";//判断是去详情显示页面还是编辑页面
response.sendRedirect("supplier_" + to + ".jsp");
} else {//默认去列表页面
this.redirectList(request, response);
}
}
/**
* 处理Get请求
*
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);//Get请求和Post请求的处理是一样的,所以把request、response转交给Post方法就好
}
/**
* 根据参数,查询出条例条件的记录集合,最后将数据返回给调用处或者将数据集合设置到session域里,再跳转到对应的列表页面
*
* @param request
* @param response
*/
private void redirectList(HttpServletRequest request, HttpServletResponse response) throws IOException {
//查询列和关键字
String searchColumn = request.getParameter("searchColumn");
String keyword = request.getParameter("keyword");
Map<String, Object> params = new HashMap();//用来保存控制层传进来的参数(查询条件)
params.put("searchColumn", searchColumn);//要查询的列
params.put("keyword", keyword/*keyword != null ? new String(keyword.getBytes("ISO-8859-1"), "UTF-8") : null*/);//查询的关键字
//User loginUser = (User) request.getSession().getAttribute("loginUser");
//if (!"管理员".equals(loginUser.getUserType())) {
// params.put("createBy", loginUser.getId());
//}
SupplierService supplierService = new SupplierServiceImpl();
response.getWriter().println(com.alibaba.fastjson.JSONObject.toJSONString(supplierService.list(params).get("list")));
}
}
OutputStream os = response.getOutputStream();
ImageIO.write(image, "JPEG", os);// 以JPEG格式向客户端发送图形验证码
} else if ("resetPassword".equalsIgnoreCase(action)) {
String msg;
User loginUser = (User) request.getSession().getAttribute("loginUser");
String oldPassword = request.getParameter("oldPassword");
if (!loginUser.getPassword().equals(oldPassword)) {
msg = "原密码错误!";
} else {
String newPassword = request.getParameter("newPassword");
loginUser.setPassword(newPassword);
UserService userService = new UserServiceImpl();
userService.update(loginUser);
msg = "修改成功!";
}
request.getSession().setAttribute("alert_msg", msg);
request.getRequestDispatcher("reset_password.jsp").forward(request, response);
} else {
response.sendRedirect("login.jsp");
}
}
/**
* 处理Get请求
*
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
// 返回一个随机颜色(Color对象)
private Color getRandomColor(int minColor, int maxColor) {
Random random = new Random();
// 保存minColor最大不会超过255
if (minColor > 255)
minColor = 255;
// 保存minColor最大不会超过255
if (maxColor > 255)
maxColor = 255;
// 获得红色的随机颜色值
int red = minColor + random.nextInt(maxColor - minColor);
// 获得绿色的随机颜色值
int green = minColor + random.nextInt(maxColor - minColor);
// 获得蓝色的随机颜色值
int blue = minColor + random.nextInt(maxColor - minColor);
return new Color(red, green, blue);
}
}
if (minColor > 255)
minColor = 255;
// 保存minColor最大不会超过255
if (maxColor > 255)
maxColor = 255;
// 获得红色的随机颜色值
int red = minColor + random.nextInt(maxColor - minColor);
// 获得绿色的随机颜色值
int green = minColor + random.nextInt(maxColor - minColor);
// 获得蓝色的随机颜色值
int blue = minColor + random.nextInt(maxColor - minColor);
return new Color(red, green, blue);
}
}
/**
* 商品模块的Servlet控制层,负责接收页面传过来的请求参数,根据action参数的值来确定页面要执行的具体操作<br>
* 而后再调用GoodService业务层的方法来处理具体的业务,最后将处理完成的结果返回或跳转至相应页面
*/
//@WebServlet("/GoodServlet")
public class GoodServlet extends HttpServlet {
/**
* 处理Post请求
*
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//过滤编码
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
String action = request.getParameter("action");
if ("add".equals(action)) {//增加
/**
* 处理Get请求
*
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);//Get请求和Post请求的处理是一样的,所以把request、response转交给Post方法就好
}
/**
* 根据参数,查询出条例条件的记录集合,最后将数据返回给调用处或者将数据集合设置到session域里,再跳转到对应的列表页面
*
* @param request
* @param response
*/
private void redirectList(HttpServletRequest request, HttpServletResponse response) throws IOException {
//查询列和关键字
String searchColumn = request.getParameter("searchColumn");
String keyword = request.getParameter("keyword");
Map<String, Object> params = new HashMap();//用来保存控制层传进来的参数(查询条件)
params.put("searchColumn", searchColumn);//要查询的列
params.put("keyword", keyword/*keyword != null ? new String(keyword.getBytes("ISO-8859-1"), "UTF-8") : null*/);//查询的关键字
//User loginUser = (User) request.getSession().getAttribute("loginUser");
//if (!"管理员".equals(loginUser.getUserType())) {
// params.put("createBy", loginUser.getId());
//}
OrderService orderService = new OrderServiceImpl();
response.getWriter().println(com.alibaba.fastjson.JSONObject.toJSONString(orderService.list(params).get("list")));
}
}
int width = 90, height = 20;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();// 获得用于输出文字的Graphics对象
Random random = new Random();
g.setColor(getRandomColor(180, 250));// 随机设置要填充的颜色
g.fillRect(0, 0, width, height);// 填充图形背景
// 设置初始字体
g.setFont(new Font("Times New Roman", Font.ITALIC, height));
g.setColor(getRandomColor(120, 180));// 随机设置字体颜色
// 用于保存最后随机生成的验证码
StringBuilder validationCode = new StringBuilder();
// 验证码的随机字体
String[] fontNames = {"Times New Roman", "Book antiqua", "Arial"};
for (int i = 0; i < 4; i++) {
// 随机设置当前验证码的字符的字体
g.setFont(new Font(fontNames[random.nextInt(3)], Font.ITALIC, height));
// 随机获得当前验证码的字符
char codeChar = codeChars.charAt(random.nextInt(charsLength));
validationCode.append(codeChar);
// 随机设置当前验证码字符的颜色
g.setColor(getRandomColor(10, 100));
// 在图形上输出验证码字符,x和y都是随机生成的
g.drawString(String.valueOf(codeChar), 16 * i + random.nextInt(7), height - random.nextInt(6));
}
HttpSession session = request.getSession();
session.setMaxInactiveInterval(5 * 60);
// 将验证码保存在session对象中,key为validation_code
session.setAttribute("validationCode", validationCode.toString());
g.dispose();// 关闭Graphics对象
OutputStream os = response.getOutputStream();
ImageIO.write(image, "JPEG", os);// 以JPEG格式向客户端发送图形验证码
} else if ("resetPassword".equalsIgnoreCase(action)) {
String msg;
User loginUser = (User) request.getSession().getAttribute("loginUser");
String oldPassword = request.getParameter("oldPassword");
if (!loginUser.getPassword().equals(oldPassword)) {
msg = "原密码错误!";
} else {
String newPassword = request.getParameter("newPassword");
loginUser.setPassword(newPassword);
UserService userService = new UserServiceImpl();
userService.update(loginUser);
msg = "修改成功!";
}
request.getSession().setAttribute("alert_msg", msg);
request.getRequestDispatcher("reset_password.jsp").forward(request, response);
/**
* 处理Post请求
*
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//过滤编码
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
String action = request.getParameter("action");
if ("add".equals(action)) {//增加
Notice vo = new Notice();
//取出页面传进来的各个数据,并设置到Notice对象的属性里
vo.setNoticeName(request.getParameter("noticeName"));
vo.setNoticeText(request.getParameter("noticeText"));
vo.setNoticeType(request.getParameter("noticeType"));
vo.setCreateDate(request.getParameter("createDate"));
vo.setCreateBy(((com.demo.vo.User) request.getSession().getAttribute("loginUser")).getId());
NoticeService noticeService = new NoticeServiceImpl();
//调用Service层增加方法(add),增加记录
noticeService.add(vo);
this.redirectList(request, response);
} else if ("delete".equals(action)) {//删除
//取出表要删除的公告记录的主键
long id = Long.parseLong(request.getParameter("id"));
NoticeService noticeService = new NoticeServiceImpl();
//调用Service层删除方法(delete),将对应的记录删除
noticeService.delete(id);
this.redirectList(request, response);
} else if ("edit".equals(action)) {//修改
//取出页面传进来的各个数据,并设置到Notice对象的属性里
Notice vo = new Notice();
vo.setId(Long.valueOf(request.getParameter("id")));
vo.setNoticeName(request.getParameter("noticeName"));
vo.setNoticeText(request.getParameter("noticeText"));
vo.setNoticeType(request.getParameter("noticeType"));
vo.setCreateDate(request.getParameter("createDate"));
vo.setCreateBy(Long.valueOf(request.getParameter("createBy")));
NoticeService noticeService = new NoticeServiceImpl();
//调用Service层更新方法(update),更新记录
noticeService.update(vo);
this.redirectList(request, response);
} else if ("get".equalsIgnoreCase(action) || "editPre".equalsIgnoreCase(action)) {//根据主键ID,查询详情信息并跳转到详情页面或编辑页面
msg = "原密码错误!";
} else {
String newPassword = request.getParameter("newPassword");
loginUser.setPassword(newPassword);
UserService userService = new UserServiceImpl();
userService.update(loginUser);
msg = "修改成功!";
}
request.getSession().setAttribute("alert_msg", msg);
request.getRequestDispatcher("reset_password.jsp").forward(request, response);
} else {
response.sendRedirect("login.jsp");
}
}
/**
* 处理Get请求
*
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
// 返回一个随机颜色(Color对象)
private Color getRandomColor(int minColor, int maxColor) {
Random random = new Random();
// 保存minColor最大不会超过255
if (minColor > 255)
minColor = 255;
// 保存minColor最大不会超过255
if (maxColor > 255)
maxColor = 255;
// 获得红色的随机颜色值
int red = minColor + random.nextInt(maxColor - minColor);
// 获得绿色的随机颜色值
int green = minColor + random.nextInt(maxColor - minColor);
// 获得蓝色的随机颜色值
int blue = minColor + random.nextInt(maxColor - minColor);
return new Color(red, green, blue);
}
}
vo.setOrderJine(request.getParameter("orderJine"));
vo.setOrderPay(request.getParameter("orderPay"));
vo.setOrderTime(request.getParameter("orderTime"));
vo.setCreateBy(((com.demo.vo.User) request.getSession().getAttribute("loginUser")).getId());
OrderService orderService = new OrderServiceImpl();
//调用Service层增加方法(add),增加记录
orderService.add(vo);
this.redirectList(request, response);
} else if ("delete".equals(action)) {//删除
//取出表要删除的订单记录的主键
long id = Long.parseLong(request.getParameter("id"));
OrderService orderService = new OrderServiceImpl();
//调用Service层删除方法(delete),将对应的记录删除
orderService.delete(id);
this.redirectList(request, response);
} else if ("edit".equals(action)) {//修改
//取出页面传进来的各个数据,并设置到Order对象的属性里
Order vo = new Order();
vo.setId(Long.valueOf(request.getParameter("id")));
vo.setOrderNo(request.getParameter("orderNo"));
vo.setOrderName(request.getParameter("orderName"));
vo.setOrderCount(request.getParameter("orderCount"));
vo.setOrderJine(request.getParameter("orderJine"));
vo.setOrderPay(request.getParameter("orderPay"));
vo.setOrderTime(request.getParameter("orderTime"));
vo.setCreateBy(Long.valueOf(request.getParameter("createBy")));
OrderService orderService = new OrderServiceImpl();
//调用Service层更新方法(update),更新记录
orderService.update(vo);
this.redirectList(request, response);
} else if ("get".equalsIgnoreCase(action) || "editPre".equalsIgnoreCase(action)) {//根据主键ID,查询详情信息并跳转到详情页面或编辑页面
Serializable id = request.getParameter("id");//取出页面传入的主键,用于查询详情
OrderService orderService = new OrderServiceImpl();
Order vo = orderService.get(id);
request.getSession().setAttribute("vo", vo);
String to = "get".equalsIgnoreCase(action) ? "info" : "edit";//判断是去详情显示页面还是编辑页面
response.sendRedirect("order_" + to + ".jsp");
} else {//默认去列表页面
this.redirectList(request, response);
}
}
/**
* 处理Get请求
*
* @param request
* @param response
* @throws ServletException
vo.setGoodType(request.getParameter("goodType"));
vo.setGoodSuppiler(request.getParameter("goodSuppiler"));
vo.setGoodText(request.getParameter("goodText"));
vo.setGoodTime(request.getParameter("goodTime"));
vo.setCreateBy(Long.valueOf(request.getParameter("createBy")));
GoodService goodService = new GoodServiceImpl();
//调用Service层更新方法(update),更新记录
goodService.update(vo);
this.redirectList(request, response);
} else if ("get".equalsIgnoreCase(action) || "editPre".equalsIgnoreCase(action)) {//根据主键ID,查询详情信息并跳转到详情页面或编辑页面
Serializable id = request.getParameter("id");//取出页面传入的主键,用于查询详情
GoodService goodService = new GoodServiceImpl();
Good vo = goodService.get(id);
request.getSession().setAttribute("vo", vo);
String to = "get".equalsIgnoreCase(action) ? "info" : "edit";//判断是去详情显示页面还是编辑页面
response.sendRedirect("good_" + to + ".jsp");
} else {//默认去列表页面
this.redirectList(request, response);
}
}
/**
* 处理Get请求
*
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);//Get请求和Post请求的处理是一样的,所以把request、response转交给Post方法就好
}
/**
* 根据参数,查询出条例条件的记录集合,最后将数据返回给调用处或者将数据集合设置到session域里,再跳转到对应的列表页面
*
* @param request
* @param response
*/
private void redirectList(HttpServletRequest request, HttpServletResponse response) throws IOException {
//查询列和关键字
String searchColumn = request.getParameter("searchColumn");
String keyword = request.getParameter("keyword");
Map<String, Object> params = new HashMap();//用来保存控制层传进来的参数(查询条件)
params.put("searchColumn", searchColumn);//要查询的列
params.put("keyword", keyword/*keyword != null ? new String(keyword.getBytes("ISO-8859-1"), "UTF-8") : null*/);//查询的关键字
String username = request.getParameter("username");
String password = request.getParameter("password");
String validationCode = request.getParameter("validationCode");
if (validationCode != null && !validationCode.equals(request.getSession().getAttribute("validationCode"))) {//验证码不通过
request.getSession().setAttribute("alert_msg", "错误:验证码不正确!");
request.getRequestDispatcher("login.jsp").forward(request, response);
return;
}
UserService userService = new UserServiceImpl();
Map<String, Object> params = new HashMap();
List<User> list = (List<User>) userService.list(params).get("list");
for (User user : list) {
if (user.getUsername().equals(username) && user.getPassword().equals(password)) {//找到这个管理员了
request.getSession().setAttribute("loginUser", user);
request.getSession().setMaxInactiveInterval(Integer.MAX_VALUE);
request.getRequestDispatcher("user_list.jsp").forward(request, response);
return;
}
}
request.getSession().setAttribute("alert_msg", "错误:用户名或密码错误!");
request.getRequestDispatcher("login.jsp").forward(request, response);
} else if ("register".equalsIgnoreCase(action)) {//注册
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println("username=" + username);
System.out.println("password=" + password);
UserService userService = new UserServiceImpl();
Map<String, Object> params = new HashMap();
params.put("startIndex", 0);
params.put("pageSize", Long.MAX_VALUE);
List<User> list = (List<User>) userService.list(params).get("list");
for (User user : list) {
if (user.getUsername().equals(username) /*&& user.getPassword().equals(password)*/) {//说明该用户名已存在,必须换个用户名才能注册
request.getSession().setAttribute("alert_msg", "错误:用户名已存在!");
request.getRequestDispatcher("register.jsp").forward(request, response);
return;
}
}
User vo = new User();
vo.setUsername(username);
vo.setPassword(password);
vo.setCreateBy(0L);
vo.setUserType("普通用户");
userService.add(vo);
request.getSession().setAttribute("alert_msg", "注册成功!用户名:[" + username + "]");
request.getRequestDispatcher("login.jsp").forward(request, response);
} else if ("logout".equalsIgnoreCase(action)) {//登出
HttpSession session = request.getSession();
response.setCharacterEncoding("UTF-8");
String action = request.getParameter("action");
if ("add".equals(action)) {//增加
Supplier vo = new Supplier();
//取出页面传进来的各个数据,并设置到Supplier对象的属性里
vo.setSupplierName(request.getParameter("supplierName"));
vo.setSupplierMan(request.getParameter("supplierMan"));
vo.setSupplierSex(request.getParameter("supplierSex"));
vo.setSupplierTel(request.getParameter("supplierTel"));
vo.setSupplierAddress(request.getParameter("supplierAddress"));
vo.setCreateBy(((com.demo.vo.User) request.getSession().getAttribute("loginUser")).getId());
SupplierService supplierService = new SupplierServiceImpl();
//调用Service层增加方法(add),增加记录
supplierService.add(vo);
this.redirectList(request, response);
} else if ("delete".equals(action)) {//删除
//取出表要删除的供应商记录的主键
long id = Long.parseLong(request.getParameter("id"));
SupplierService supplierService = new SupplierServiceImpl();
//调用Service层删除方法(delete),将对应的记录删除
supplierService.delete(id);
this.redirectList(request, response);
} else if ("edit".equals(action)) {//修改
//取出页面传进来的各个数据,并设置到Supplier对象的属性里
Supplier vo = new Supplier();
vo.setId(Long.valueOf(request.getParameter("id")));
vo.setSupplierName(request.getParameter("supplierName"));
vo.setSupplierMan(request.getParameter("supplierMan"));
vo.setSupplierSex(request.getParameter("supplierSex"));
vo.setSupplierTel(request.getParameter("supplierTel"));
vo.setSupplierAddress(request.getParameter("supplierAddress"));
vo.setCreateBy(Long.valueOf(request.getParameter("createBy")));
SupplierService supplierService = new SupplierServiceImpl();
//调用Service层更新方法(update),更新记录
supplierService.update(vo);
this.redirectList(request, response);
} else if ("get".equalsIgnoreCase(action) || "editPre".equalsIgnoreCase(action)) {//根据主键ID,查询详情信息并跳转到详情页面或编辑页面
Serializable id = request.getParameter("id");//取出页面传入的主键,用于查询详情
SupplierService supplierService = new SupplierServiceImpl();
Supplier vo = supplierService.get(id);
String keyword = request.getParameter("keyword");
Map<String, Object> params = new HashMap();//用来保存控制层传进来的参数(查询条件)
params.put("searchColumn", searchColumn);//要查询的列
params.put("keyword", keyword/*keyword != null ? new String(keyword.getBytes("ISO-8859-1"), "UTF-8") : null*/);//查询的关键字
//User loginUser = (User) request.getSession().getAttribute("loginUser");
//if (!"管理员".equals(loginUser.getUserType())) {
// params.put("createBy", loginUser.getId());
//}
GoodService goodService = new GoodServiceImpl();
response.getWriter().println(com.alibaba.fastjson.JSONObject.toJSONString(goodService.list(params).get("list")));
}
}
/**
* 用户模块的Servlet控制层,负责接收页面传过来的请求参数,根据action参数的值来确定页面要执行的具体操作<br>
* 而后再调用UserService业务层的方法来处理具体的业务,最后将处理完成的结果返回或跳转至相应页面
*/
//@WebServlet("/UserServlet")
public class UserServlet extends HttpServlet {
/**
* 处理Post请求
*
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//过滤编码
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
//@WebServlet("/AuthServlet")
public class AuthServlet extends HttpServlet {
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//过滤编码
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
String action = request.getParameter("action");
if ("login".equalsIgnoreCase(action)) {//登录
String username = request.getParameter("username");
String password = request.getParameter("password");
String validationCode = request.getParameter("validationCode");
if (validationCode != null && !validationCode.equals(request.getSession().getAttribute("validationCode"))) {//验证码不通过
request.getSession().setAttribute("alert_msg", "错误:验证码不正确!");
request.getRequestDispatcher("login.jsp").forward(request, response);
return;
}
UserService userService = new UserServiceImpl();
Map<String, Object> params = new HashMap();
List<User> list = (List<User>) userService.list(params).get("list");
for (User user : list) {
if (user.getUsername().equals(username) && user.getPassword().equals(password)) {//找到这个管理员了
request.getSession().setAttribute("loginUser", user);
request.getSession().setMaxInactiveInterval(Integer.MAX_VALUE);
request.getRequestDispatcher("user_list.jsp").forward(request, response);
return;
}
}
request.getSession().setAttribute("alert_msg", "错误:用户名或密码错误!");
request.getRequestDispatcher("login.jsp").forward(request, response);
} else if ("register".equalsIgnoreCase(action)) {//注册
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println("username=" + username);
/**
* 根据参数,查询出条例条件的记录集合,最后将数据返回给调用处或者将数据集合设置到session域里,再跳转到对应的列表页面
*
* @param request
* @param response
*/
private void redirectList(HttpServletRequest request, HttpServletResponse response) throws IOException {
//查询列和关键字
String searchColumn = request.getParameter("searchColumn");
String keyword = request.getParameter("keyword");
Map<String, Object> params = new HashMap();//用来保存控制层传进来的参数(查询条件)
params.put("searchColumn", searchColumn);//要查询的列
params.put("keyword", keyword/*keyword != null ? new String(keyword.getBytes("ISO-8859-1"), "UTF-8") : null*/);//查询的关键字
//User loginUser = (User) request.getSession().getAttribute("loginUser");
//if (!"管理员".equals(loginUser.getUserType())) {
// params.put("createBy", loginUser.getId());
//}
GoodService goodService = new GoodServiceImpl();
response.getWriter().println(com.alibaba.fastjson.JSONObject.toJSONString(goodService.list(params).get("list")));
}
}
/**
* 用户模块的Servlet控制层,负责接收页面传过来的请求参数,根据action参数的值来确定页面要执行的具体操作<br>
* 而后再调用UserService业务层的方法来处理具体的业务,最后将处理完成的结果返回或跳转至相应页面
*/
}
/**
* 处理Get请求
*
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);//Get请求和Post请求的处理是一样的,所以把request、response转交给Post方法就好
}
/**
* 根据参数,查询出条例条件的记录集合,最后将数据返回给调用处或者将数据集合设置到session域里,再跳转到对应的列表页面
*
* @param request
* @param response
*/
private void redirectList(HttpServletRequest request, HttpServletResponse response) throws IOException {
//查询列和关键字
String searchColumn = request.getParameter("searchColumn");
String keyword = request.getParameter("keyword");
Map<String, Object> params = new HashMap();//用来保存控制层传进来的参数(查询条件)
params.put("searchColumn", searchColumn);//要查询的列
params.put("keyword", keyword/*keyword != null ? new String(keyword.getBytes("ISO-8859-1"), "UTF-8") : null*/);//查询的关键字
User loginUser = (User) request.getSession().getAttribute("loginUser");
if (!"管理员".equals(loginUser.getUserType())) {
params.put("id", loginUser.getId());
}
UserService userService = new UserServiceImpl();
response.getWriter().println(com.alibaba.fastjson.JSONObject.toJSONString(userService.list(params).get("list")));
}
}
/**
* 订单模块的Servlet控制层,负责接收页面传过来的请求参数,根据action参数的值来确定页面要执行的具体操作<br>
* 而后再调用OrderService业务层的方法来处理具体的业务,最后将处理完成的结果返回或跳转至相应页面
*/
//@WebServlet("/OrderServlet")
public class OrderServlet extends HttpServlet {
/**
* 处理Post请求
*
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//过滤编码
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
String action = request.getParameter("action");
if ("add".equals(action)) {//增加
Order vo = new Order();
//取出页面传进来的各个数据,并设置到Order对象的属性里
vo.setOrderNo(request.getParameter("orderNo"));
vo.setOrderName(request.getParameter("orderName"));
vo.setOrderCount(request.getParameter("orderCount"));
vo.setOrderJine(request.getParameter("orderJine"));
vo.setOrderPay(request.getParameter("orderPay"));
vo.setOrderTime(request.getParameter("orderTime"));
vo.setCreateBy(((com.demo.vo.User) request.getSession().getAttribute("loginUser")).getId());
OrderService orderService = new OrderServiceImpl();
//调用Service层增加方法(add),增加记录
orderService.add(vo);
this.redirectList(request, response);
} else if ("delete".equals(action)) {//删除
//取出表要删除的订单记录的主键
long id = Long.parseLong(request.getParameter("id"));
OrderService orderService = new OrderServiceImpl();
//调用Service层删除方法(delete),将对应的记录删除
orderService.delete(id);
this.redirectList(request, response);
long id = Long.parseLong(request.getParameter("id"));
GoodService goodService = new GoodServiceImpl();
//调用Service层删除方法(delete),将对应的记录删除
goodService.delete(id);
this.redirectList(request, response);
} else if ("edit".equals(action)) {//修改
//取出页面传进来的各个数据,并设置到Good对象的属性里
Good vo = new Good();
vo.setId(Long.valueOf(request.getParameter("id")));
vo.setGoodName(request.getParameter("goodName"));
vo.setGoodCount(request.getParameter("goodCount"));
vo.setGoodPrice(request.getParameter("goodPrice"));
vo.setGoodType(request.getParameter("goodType"));
vo.setGoodSuppiler(request.getParameter("goodSuppiler"));
vo.setGoodText(request.getParameter("goodText"));
vo.setGoodTime(request.getParameter("goodTime"));
vo.setCreateBy(Long.valueOf(request.getParameter("createBy")));
GoodService goodService = new GoodServiceImpl();
//调用Service层更新方法(update),更新记录
goodService.update(vo);
this.redirectList(request, response);
} else if ("get".equalsIgnoreCase(action) || "editPre".equalsIgnoreCase(action)) {//根据主键ID,查询详情信息并跳转到详情页面或编辑页面
Serializable id = request.getParameter("id");//取出页面传入的主键,用于查询详情
GoodService goodService = new GoodServiceImpl();
Good vo = goodService.get(id);
request.getSession().setAttribute("vo", vo);
String to = "get".equalsIgnoreCase(action) ? "info" : "edit";//判断是去详情显示页面还是编辑页面
response.sendRedirect("good_" + to + ".jsp");
} else {//默认去列表页面
this.redirectList(request, response);
}
}
/**
* 处理Get请求
*
* @param request
* @param response
* @throws ServletException
* @throws IOException