概述
按照需求设计,我们希望商铺管理者
- 店铺名称不能修改
- 店铺类别不能修改
- 其余信息可编辑修改
分为两步,
- 第一步前端传入shopId到后台获取商铺信息,前端展示
- 第二步修改商铺信息,提交到后台,更新商铺
ShopController
com.artisan.o2o.web.shopadmin.ShopController.java
ShopController#getShopInfoById方法
getShopInfoById
/** * * * @Title: getShopInfoById * * @Description: 根据shopId获取shop信息, 接收前端的请求,获取shopId ,所以入参为HttpServletRequest * @ResponseBody 不需要VIEW展现层模块,直接显示到客户端的内容。 将内容或对象作为 HTTP 响应正文返回 * * @param request * * @return: Map<String,Object> */ @RequestMapping(value = "/getshopinfobyId", method = RequestMethod.GET) @ResponseBody public Map<String, Object> getShopInfoById(HttpServletRequest request) { Map<String, Object> modelMap = new HashMap<String, Object>(); // shopId 为和前端约定好的变量 int shopId = HttPServletRequestUtil.getInt(request, "shopId"); try { if (shopId >= 0) { // 查询 ,按照设计,我们希望前端页面下拉列表中可以修改区域信息,所以需要查询出来全量的区域列表 // 对已ShopCategory而言,我们DAO层的SQL已经将shop_category_id和 // shop_category_name 查询出来,前端设置到对应的属性上即可。没有必要全部查询出来。 Shop shop = shopService.getShopById(shopId); List<Area> areaList = areaservice.getAreaList(); modelMap.put("success", true); modelMap.put("shop", shop); modelMap.put("areaList", areaList); } else { modelMap.put("success", false); modelMap.put("errMsg", "shopId不合法"); } } catch (Exception e) { modelMap.put("success", false); modelMap.put("errMsg", e.getMessage()); } return modelMap; }
测试
启动tomcat,访问 http://localhost:8080/o2o/shopadmin/getshopinfobyId?shopId=30
返回的数据如下:
{ "shop": { "shopId": 30, "shopName": "优乐美", "shopDesc": "优乐美奶茶店", "shopAddr": "复兴街", "phone": "123456", "shopImg": "\\upload\\item\\shopImage\\30\\2018053001010899137.png", "priority": null, "createTime": 1527656467000, "lastEditTime": 1527656467000, "enableStatus": 0, "advice": null, "owner": null, "area": { "areaId": 1, "areaName": "北京", "areaDesc": null, "priority": null, "createTime": null, "lastEditTime": null }, "shopCategory": { "shopCategoryId": 3, "shopCategoryName": "奶茶", "shopCategoryDesc": null, "shopCategoryImg": null, "priority": null, "createTime": null, "lastEditTime": null, "parent": null } }, "success": true, "areaList": [ { "areaId": 2, "areaName": "上海", "areaDesc": "魔都", "priority": 99, "createTime": 1526259636000, "lastEditTime": 1526346041000 }, { "areaId": 1, "areaName": "北京", "areaDesc": "帝都", "priority": 0, "createTime": 1526259626000, "lastEditTime": 1526346033000 } ] }
检查数据,符合预期。
ShopController#modifyShop方法
/** * * * @Title: modifyshop * * @Description: * * @param request * 因为是接收前端的请求,而前端的信息都封装在HttpServletRequest中, * 所以需要解析HttpServletRequest,获取必要的参数 * * 1. 接收并转换相应的参数,包括shop信息和图片信息 2.修改店铺 3. 返回结果给前台 * @return * * @return: Map<String,Object> */ @RequestMapping(value = "/modifyshop", method = RequestMethod.POST) @ResponseBody public Map<String, Object> modifyshop(HttpServletRequest request) { Map<String, Object> modelMap = new HashMap<String, Object>(); // 0. 验证码校验 if (!VerifyCodeUtil.verifyCode(request)) { modelMap.put("success", false); modelMap.put("errMsg", "验证码不正确"); return modelMap; } // 1. 接收并转换相应的参数,包括shop信息和图片信息 // 1.1 shop信息 // shopStr 是和前端约定好的参数值,后端从request中获取request这个值来获取shop的信息 String shopStr = HttPServletRequestUtil.getString(request, "shopStr"); // 使用jackson-databind 将json转换为pojo ObjectMapper mapper = new ObjectMapper(); Shop shop = null; try { // 将json转换为pojo shop = mapper.readValue(shopStr, Shop.class); } catch (Exception e) { e.printStackTrace(); // 将错误信息返回给前台 modelMap.put("success", false); modelMap.put("errMsg", e.getMessage()); return modelMap; } // 1.2 图片信息 基于Apache Commons FileUpload的文件上传 ( 修改商铺信息 图片可以不更新) // Spring MVC中的 图片存在CommonsMultipartFile中 CommonsMultipartFile shopImg = null; // 从request的本次会话中的上线文中获取图片的相关内容 CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext()); if (commonsMultipartResolver.isMultipart(request)) { MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; // shopImg是和前端约定好的变量名 shopImg = (CommonsMultipartFile) multipartRequest.getFile("shopImg"); } // 2. 修改店铺 if (shop != null && shop.getShopId() != null) { // Session 部分的 PersonInfo 修改商铺是不需要的设置的。 // 修改店铺 ShopExecution se = null; try { if (shopImg != null) { se = shopService.modifyShop(shop, shopImg.getInputStream(), shopImg.getOriginalFilename()); } else { se = shopService.modifyShop(shop, null, null); } // 成功 if (se.getState() == ShopStateEnum.SUCCESS.getState()) { modelMap.put("success", true); modelMap.put("errMsg", "修改成功"); } else { modelMap.put("success", false); modelMap.put("errMsg", se.getStateInfo()); } } catch (Exception e) { e.printStackTrace(); modelMap.put("success", false); modelMap.put("errMsg", "ModifyShop Error"); } } else { // 将错误信息返回给前台 modelMap.put("success", false); modelMap.put("errMsg", "ShopId不合法"); } return modelMap; }
Controller层的单元测试,待我们完成前端页面一起联调。
注册商铺的session部分的完善
同时 完善下 注册商铺的session部分,之前是写死的person ,现在需要从session中获取, 具体看注释部分。
// 2. 注册店铺 if (shop != null && shopImg != null) { // Session // 店主persionInfo的信息,肯定要登录才能注册店铺。 // 所以这部分信息我们从session中获取,尽量不依赖前端,这里暂时时不具备条件,后续改造,先硬编码,方便单元测试 // PersonInfo personInfo = new PersonInfo(); // personInfo.setUserId(1L); // 注册店铺之前登录,登录成功后,约定好将user这个key 设置到session中, // 这里通过key就可以取到PersonInfo的信息 PersonInfo personInfo = (PersonInfo) request.getSession().getAttribute("user"); shop.setOwner(personInfo);
Github地址
代码地址: https://github.com/yangshangwei/o2o