Java实现茶叶售卖商城系统(java+SSM+JSP+EasyUi+mysql)
程序员文章站
2022-03-05 16:34:51
目录前言实现效果主要代码用户管理控制层商品管理服务类订单控制层前言这是一个应用ssm框架的项目,前端页面整洁清晰。该系统有两个角色,一个是普通用户,另一个是管理员。普通用户具有注册、登录、查看商品、添...
前言
这是一个应用ssm框架的项目,前端页面整洁清晰。该系统有两个角色,一个是普通用户,另一个是管理员。
普通用户具有注册、登录、查看商品、添加购物车、添加商品收藏、下订单、商品评价、用户地址管理等等功能。
管理员具有登录、管理用户信息、管理商品信息、管理商品活动信息、管理订单信息、管理用户评论信息的等等功能。
应用技术:jsp + ssm + easyui
运行环境:eclipse/idea + mysql5.7 + tomcat8.5 + jdk1.8
实现效果
主要代码
用户管理控制层
@webservlet("/frontstage_userservlet") public class userservlet extends httpservlet { userservice service = new userservice(); @override protected void service(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception { string action = req.getparameter("action"); switch (action) { case "login": login(req, resp); break; case "checkusernamerepeat": checkusernamerepeat(req, resp); break; case "register": register(req, resp); break; case "logout": logout(req, resp); break; case "checkoldpassword": checkoldpassword(req, resp); break; case "modifypassword": modifypassword(req, resp); break; } } public void login(httpservletrequest req, httpservletresponse resp) throws ioexception { //设置请求编码格式: req.setcharacterencoding("utf-8"); //设置响应编码格式 resp.setcontenttype("text/html;charset=utf-8"); string username = req.getparameter("username"); string password = req.getparameter("password"); string captcha = req.getparameter("captcha"); if (captcha != null) { captcha = captcha.touppercase(); } // 先进行验证码验证 string checkcode = (string) req.getsession().getattribute("checkcode_session"); try { // 将用户输入的验证码和 系统验证对比 if (checkcode.equals(captcha)) { user user = service.getuserbynameandpassword(username, password); if (user != null) { httpsession session = req.getsession(); session.setattribute("user", user); jsonresult ok = jsonresult.ok(); resp.getwriter().println(jsonutil.javaobjecttojson(ok)); } else { // 将错误信息封装在结果集中 jsonresult result = jsonresult.errormsg("用户名或密码错误,请重试"); // 以json的形式返回给前端 resp.getwriter().println(jsonutil.javaobjecttojson(result)); } } else { // 将错误信息封装在结果集中 jsonresult result = jsonresult.errormsg("验证码输入错误,请重试"); // 以json的形式返回给前端 resp.getwriter().println(jsonutil.javaobjecttojson(result)); } } catch (exception e) { e.printstacktrace(); } } /** * 用户名重名检测 * * @param req * @param resp */ public void checkusernamerepeat(httpservletrequest req, httpservletresponse resp) { string username = req.getparameter("username"); if (username != null) { int d = service.checkusernamerepeat(username); try { if (d > 0) { jsonresult error = jsonresult.errormsg(""); resp.getwriter().println(jsonutil.javaobjecttojson(error)); } else { jsonresult ok = jsonresult.ok(); resp.getwriter().println(jsonutil.javaobjecttojson(ok)); } } catch (exception e) { e.printstacktrace(); } } } /** * 用户注册 * * @param req * @param resp * @throws ioexception */ public void register(httpservletrequest req, httpservletresponse resp) throws ioexception { //设置请求编码格式: req.setcharacterencoding("utf-8"); //设置响应编码格式 resp.setcontenttype("text/html;charset=utf-8"); user user = new user(); user.setusername(req.getparameter("username")); user.setpassword(req.getparameter("password")); user.setemail(req.getparameter("email")); user.setphone(req.getparameter("phonenum")); user.setrole("ordinaryuser"); int d = service.adduser(user); try { if (d > 0) { jsonresult ok = jsonresult.ok(); resp.getwriter().println(jsonutil.javaobjecttojson(ok)); } else { jsonresult error = jsonresult.errormsg("注册失败"); resp.getwriter().println(jsonutil.javaobjecttojson(error)); } } catch (exception e) { e.printstacktrace(); } } /** * 退出登录 */ public void logout(httpservletrequest req, httpservletresponse resp) { req.getsession().removeattribute("user"); try { resp.sendredirect("index.jsp"); } catch (ioexception e) { e.printstacktrace(); } } /** * 检查旧密码是否正确 ,在修改密码操作时使用 * @throws ioexception */ public void checkoldpassword(httpservletrequest req, httpservletresponse resp) throws ioexception { //设置请求编码格式: req.setcharacterencoding("utf-8"); //设置响应编码格式 resp.setcontenttype("text/html;charset=utf-8"); user user = (user) req.getsession().getattribute("user"); string oldpassword = service.getpasswordbyid(user.getid()); system.out.println("旧密码:"+oldpassword); string password = req.getparameter("password"); system.out.println("旧密码验证:"+password); try { if (!oldpassword.equals(password)) { jsonresult errormsg = jsonresult.errormsg("原始密码错误,请重新输入"); resp.getwriter().println(jsonutil.javaobjecttojson(errormsg)); } else { jsonresult ok = jsonresult.ok(); resp.getwriter().println(jsonutil.javaobjecttojson(ok)); } } catch (ioexception e) { e.printstacktrace(); } } /** * 修改密码 * @throws ioexception */ public void modifypassword(httpservletrequest req, httpservletresponse resp) throws ioexception { //设置请求编码格式: req.setcharacterencoding("utf-8"); //设置响应编码格式 resp.setcontenttype("text/html;charset=utf-8"); string password = req.getparameter("newpassword"); system.out.println("新密码:"+password); user user = (user) req.getsession().getattribute("user"); int id = user.getid(); system.out.println("旧密码id"+id); int d = service.updatepasswordbyid(id, password); try { if (d > 0) { jsonresult ok = jsonresult.ok(); resp.getwriter().println(jsonutil.javaobjecttojson(ok)); } else { jsonresult errormsg = jsonresult.errormsg("修改失败,请重试"); resp.getwriter().println(jsonutil.javaobjecttojson(errormsg)); } } catch (exception e) { e.printstacktrace(); } } }
商品管理服务类
@webservlet("/frontstage_goodsservlet") public class goodsservlet extends httpservlet{ goodsservice service = new goodsservice(); @override protected void service(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception { string action = req.getparameter("action"); switch(action) { case "findallgoods":findallgoods(req,resp);break; case "findgoodsbyid":findgoodsbyid(req,resp);break; case "findgoodsbytype":findgoodsbytype(req,resp);break; } } /** * 获取所有商品列表 * @param req * @param resp */ public void findallgoods(httpservletrequest req, httpservletresponse resp) { map<string, list<goods>> allgoods = service.getallgoods(); try { req.setattribute("allgoods", allgoods); req.getrequestdispatcher("/main.jsp").forward(req, resp); return; } catch (servletexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } } /** *根据类型获得商品列表 * @param req * @param resp */ public void findgoodsbytype(httpservletrequest req, httpservletresponse resp) { string type = req.getparameter("type"); string currentpage = req.getparameter("currentpage"); pagebean pagebean = null; // 如果当前第几页currentpage 值为null,说明第一次跳转到此页面或者是要跳转到首页,则设定该值currentpage默认为1 if(currentpage == null) { pagebean = service.getgoodsbytype(type, 4, 1); }else { pagebean = service.getgoodsbytype(type, 4, integer.parseint(currentpage)); } try { req.setattribute("pagebean", pagebean); req.setattribute("type", type); req.getrequestdispatcher("/product.jsp").forward(req, resp); } catch (exception e) { e.printstacktrace(); } } /** * 获取商品的详细信息 * @param req * @param resp */ public void findgoodsbyid(httpservletrequest req, httpservletresponse resp) { int id =integer.parseint(req.getparameter("id")); goods goodsinfo = service.getgoodsinfobyid(id); try { req.setattribute("pro", goodsinfo); req.getrequestdispatcher("/productinfo_user.jsp").forward(req, resp); } catch (exception e) { e.printstacktrace(); } } }
订单控制层
@webservlet("/backstage_ordersservlet") public class ordersservlet extends httpservlet{ ordersservice service = new ordersservice(); @override protected void service(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception { string action = req.getparameter("action"); system.out.println(action); switch(action) { case "getorderslist":getorderslist(req,resp);break; case "getorderslistbyname":getorderslistbyname(req,resp);break; case "deleteorders":deleteorders(req,resp);break; case "deleteallorders":deleteallorders(req,resp);break; case "toordersupdatepage":toordersupdatepage(req,resp);break; case "updateorders":updateorders(req,resp);break; case "fastbuy":sendorder(req,resp);break; } } /* * 删除全部订单 */ private void deleteallorders(httpservletrequest req, httpservletresponse resp) throws ioexception { //设置请求编码格式: req.setcharacterencoding("utf-8"); //设置响应编码格式 resp.setcontenttype("text/html;charset=utf-8"); // todo 自动生成的方法存根 int d = service.deleteallorders(); try { if(d>0) { jsonresult ok = jsonresult.ok(); resp.getwriter().println(jsonutil.javaobjecttojson(ok)); }else { jsonresult errormsg = jsonresult.errormsg("删除失败,请重试"); resp.getwriter().println(jsonutil.javaobjecttojson(errormsg)); } } catch (exception e) { e.printstacktrace(); } } /** * 提交订单 * @throws ioexception */ public void sendorder(httpservletrequest req, httpservletresponse resp) throws ioexception { //设置请求编码格式: req.setcharacterencoding("utf-8"); //设置响应编码格式 resp.setcontenttype("text/html;charset=utf-8"); simpledateformat df = new simpledateformat("yyyy-mm-dd hh:mm:ss");//设置日期格式 user user =(user) req.getsession().getattribute("user"); orders order = new orders(); order.setnumber(order.getrandomstring(10)); order.settime(df.format(new date())); order.setname(req.getparameter("recipients")); order.setaddress(req.getparameter("address")); order.setphone(req.getparameter("phone")); order.setaddress_label(req.getparameter("addresslabel")); order.setsex(req.getparameter("sex")); order.setuser(user.getusername()); order.setgoods_id(integer.parseint(req.getparameter("id"))); order.setgoods_num(integer.parseint(req.getparameter("num"))); order.setgoods_status(1); int d = service.addorder(order); try { if(d>0) { jsonresult ok = jsonresult.ok(); resp.getwriter().println(jsonutil.javaobjecttojson(ok)); }else { jsonresult error = jsonresult.errormsg("订单提交失败!"); resp.getwriter().println(jsonutil.javaobjecttojson(error)); } } catch (exception e) { e.printstacktrace(); } } /** * 向前端页面返回订单数据列表 */ public void getorderslist(httpservletrequest req,httpservletresponse resp) { list<orders> allorders = service.getallorders(); req.setattribute("orderslist",allorders); try { req.getrequestdispatcher("/backstage/tgls/ordersmanage/orders_list.jsp").forward(req, resp); } catch (servletexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } } /** * 根据订单名查询商品(注意!类别没改) * @param req * @param resp * @throws ioexception */ public void getorderslistbyname(httpservletrequest req,httpservletresponse resp) throws ioexception { //设置请求编码格式: req.setcharacterencoding("utf-8"); //设置响应编码格式 resp.setcontenttype("text/html;charset=utf-8"); string name = req.getparameter("ordersuser"); //string type = req.getparameter("type"); map<string,string> parmas = new hashmap<>(); parmas.put("name", name); //parmas.put("type", type); list<orders> list = service.getordersbyname(parmas); try { if(list != null) { jsonresult ok = jsonresult.ok(list); resp.getwriter().println(jsonutil.javaobjecttojson(ok)); }else { jsonresult errormsg = jsonresult.errormsg("未获取到任何数据,请重试"); resp.getwriter().println(jsonutil.javaobjecttojson(errormsg)); } } catch (exception e) { e.printstacktrace(); } } /** * 修改订单时,获取此订单的全部数据,并返回至修改页面 */ public void toordersupdatepage(httpservletrequest req,httpservletresponse resp) { int id = integer.parseint(req.getparameter("id")); orders orders = service.getordersinfobyid(id); req.setattribute("orders", orders); try { req.getrequestdispatcher("/backstage/tgls/ordersmanage/orders_update.jsp").forward(req, resp); } catch (servletexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } } /** * 修改订单 * @throws ioexception */ public void updateorders(httpservletrequest req,httpservletresponse resp) throws ioexception { //设置请求编码格式: req.setcharacterencoding("utf-8"); //设置响应编码格式 resp.setcontenttype("text/html;charset=utf-8"); orders orders = new orders(); orders.setid(integer.parseint(req.getparameter("id"))); orders.setnumber(req.getparameter("number")); orders.setuser(req.getparameter("user")); orders.settime(req.getparameter("time")); orders.setname(req.getparameter("name")); orders.setsex(req.getparameter("sex")); orders.setaddress(req.getparameter("address")); orders.setphone(req.getparameter("phone")); orders.setaddress_label(req.getparameter("address_label")); orders.setgoods_id(integer.parseint(req.getparameter("goods_id"))); orders.setgoods_num(integer.parseint(req.getparameter("goods_num"))); orders.setgoods_status(integer.parseint(req.getparameter("goods_status"))); system.out.println(orders); int d = service.updateordersbyid(orders); system.out.println(d); try { if(d>0) { jsonresult ok = jsonresult.ok(); resp.getwriter().println(jsonutil.javaobjecttojson(ok)); }else { jsonresult errormsg = jsonresult.errormsg("修改失败,请重试"); resp.getwriter().println(jsonutil.javaobjecttojson(errormsg)); } } catch (exception e) { e.printstacktrace(); } } /** * 删除订单 * @throws unsupportedencodingexception */ public void deleteorders(httpservletrequest req,httpservletresponse resp) throws unsupportedencodingexception { //设置请求编码格式: req.setcharacterencoding("utf-8"); //设置响应编码格式 resp.setcontenttype("text/html;charset=utf-8"); int id = integer.parseint(req.getparameter("id")); int d = service.deleteordersbyid(id); try { if(d>0) { jsonresult ok = jsonresult.ok(); resp.getwriter().println(jsonutil.javaobjecttojson(ok)); }else { jsonresult errormsg = jsonresult.errormsg("删除失败,请重试"); resp.getwriter().println(jsonutil.javaobjecttojson(errormsg)); } } catch (exception e) { e.printstacktrace(); } } }
到此这篇关于java实现茶叶售卖商城系统(java+ssm+jsp+easyui+mysql)的文章就介绍到这了,更多相关java 茶叶售卖商城系统内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
上一篇: SpringMVC中的几个模型对象