实战SSM_O2O商铺_34【商品】商品编辑之Controller层的实现
生活随笔
收集整理的這篇文章主要介紹了
实战SSM_O2O商铺_34【商品】商品编辑之Controller层的实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 概述
- ProductController
- 單元測試
- Github地址
概述
在完成了 實戰SSM_O2O商鋪_33【商品】商品編輯之Service層的實現之后,我們繼續來實現Controller層的代碼部分。
主要邏輯:
- 根據前端url中productId ,調用service層的接口獲取product信息,以便前端能在商品編輯頁面展示對應的數據
- 修改商品
ProductController
/*** * * @Title: getProductById* * @Description: 因為只需要傳入productId,使用@RequestParam注解。* 同時也無需傳入HttpServletRequest,用不到* * 根據頁面原型,不僅要加載prodcut的信息,還需要加載對應的目錄信息。* * @param productId* * @return: Map<String,Object>*/@RequestMapping(value = "/getproductbyid", method = RequestMethod.GET)@ResponseBodypublic Map<String, Object> getProductById(@RequestParam long productId) {Map<String, Object> modelMap = new HashMap<String, Object>();if (productId > -1) {Product product = productService.queryProductById(productId);List<ProductCategory> productCategoryList = productCategoryService.queryProductCategoryList(product.getShop().getShopId());modelMap.put("product", product);modelMap.put("productCategoryList", productCategoryList);modelMap.put("success", true);} else {modelMap.put("success", false);modelMap.put("errMsg", "empty pageSize or pageIndex or shopId");}return modelMap;}@RequestMapping(value = "/modifyproduct", method = RequestMethod.POST)@ResponseBodyprivate Map<String, Object> modifyProduct(HttpServletRequest request) {Map<String, Object> modelMap = new HashMap<String, Object>();if (!VerifyCodeUtil.verifyCode(request)) {modelMap.put("success", false);modelMap.put("errMsg", "輸入了錯誤的驗證碼");return modelMap;}Product product = null;// 接收前端傳遞過來的productString productStr = null;// 商品圖片縮略圖(輸入流和名稱的封裝類)ImageHolder thumbnail = null;// 將HttpServletRequest轉型為MultipartHttpServletRequest,可以很方便地得到文件名和文件內容MultipartHttpServletRequest multipartHttpServletRequest = null;// 接收商品縮略圖CommonsMultipartFile thumbnailFile = null;// 接收商品詳情圖片List<ImageHolder> productDetailImgList = new ArrayList<ImageHolder>();// 創建一個通用的多部分解析器CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());// Step2: 使用FastJson提供的api,實例化Product 構造調用service層的第一個參數ObjectMapper mapper = new ObjectMapper();// 獲取前端傳遞過來的product,約定好使用productStrtry {productStr = HttpServletRequestUtil.getString(request, "productStr");product = mapper.readValue(productStr, Product.class);} catch (Exception e) {modelMap.put("success", false);modelMap.put("errMsg", e.toString());return modelMap;}// Step3: 商品縮略圖 和 商品詳情圖 構造調用service層的第二個參數和第三個參數try {// 判斷 request 是否有文件上傳,即多部分請求if (commonsMultipartResolver.isMultipart(request)) {// 將request轉換成多部分requestmultipartHttpServletRequest = (MultipartHttpServletRequest) request;// 得到縮略圖的CommonsMultipartFile ,和前端約定好使用thumbnail 傳遞// ,并構建ImageHolder對象thumbnailFile = (CommonsMultipartFile) multipartHttpServletRequest.getFile("thumbnail");// 轉化為ImageHolder,使用service層的參數類型要求thumbnail = new ImageHolder(thumbnailFile.getInputStream(), thumbnailFile.getOriginalFilename());// 得到 商品詳情的列表,和前端約定使用productImg + i 傳遞 ,并構建ImageHolder對象for (int i = 0; i < IMAGEMAXCOUNT; i++) {CommonsMultipartFile productDetailImgFile = (CommonsMultipartFile) multipartHttpServletRequest.getFile("productImg" + i);if (productDetailImgFile != null) {ImageHolder productDetailImg = new ImageHolder(productDetailImgFile.getInputStream(), productDetailImgFile.getOriginalFilename());productDetailImgList.add(productDetailImg);} else {// 如果從請求中獲取的到file為空,終止循環break;}}} else {modelMap.put("success", false);modelMap.put("errMsg", "上傳圖片不能為空");return modelMap;}} catch (Exception e) {e.printStackTrace();modelMap.put("success", false);modelMap.put("errMsg", e.toString());return modelMap;}// Step4 調用Service層if (product != null && thumbnailFile != null && productDetailImgList.size() > 0) {try {// 從session中獲取shop信息,不依賴前端的傳遞更加安全Shop currentShop = (Shop) request.getSession().getAttribute("currentShop");product.setShop(currentShop);// 調用addProductProductExecution pe = productService.modifyProduct(product, thumbnail, productDetailImgList);if (pe.getState() == ProductStateEnum.SUCCESS.getState()) {modelMap.put("success", true);} else {modelMap.put("success", false);modelMap.put("errMsg", pe.getStateInfo());}} catch (ProductOperationException e) {modelMap.put("success", false);modelMap.put("errMsg", e.toString());return modelMap;}} else {modelMap.put("success", false);modelMap.put("errMsg", "請輸入商品信息");}return modelMap;}/*** * * @Title: changeStatus* * @Description: 標注@ResponseBody將返回的model解析為json* * @param request* @return* * @return: Map<String,Object>*/@RequestMapping(value = "/changestatus",method = RequestMethod.POST)@ResponseBodypublic Map<String, Object> changeStatus(HttpServletRequest request) {Map<String, Object> modelMap = new HashMap<String, Object>();ObjectMapper mapper = new ObjectMapper();// 獲取前端傳遞過來的product,約定好使用productStrtry {String productStr = HttpServletRequestUtil.getString(request, "productStr");Product product = mapper.readValue(productStr, Product.class);Shop tempShop = (Shop) request.getSession().getAttribute("currentShop");product.setShop(tempShop);ProductExecution pe = productService.modifyProduct(product, null, null);if (pe.getState() == ProductStateEnum.SUCCESS.getState()) {modelMap.put("success", true);modelMap.put("errMsg", "操作成功");} else {modelMap.put("success", false);modelMap.put("errMsg", pe.getStateInfo());}} catch (Exception e) {modelMap.put("success", false);modelMap.put("errMsg", e.toString());return modelMap;}return modelMap;}單元測試
啟動tomcat,我們對getProductById方法進行單元測試。modifyProduct方法待前端開發完成后一并測試。
根據路由規則訪問
http://localhost:8080/o2o/shopadmin/getproductbyid?productId=7 {"product": {"productId": 7,"productName": "offical_product","productDesc": "product offical desc","imgAddr": "\\upload\\item\\shopImage\\5\\2018070100324625530.jpg","normalPrice": "100","promotionPrice": "80","priority": 66,"createTime": 1530286468000,"lastEditTime": 1530419566000,"enableStatus": 1,"productImgList": [{"productImgId": 14,"imgAddr": "\\upload\\item\\shopImage\\5\\2018070100324625530.jpg","imgDesc": null,"priority": 66,"createTime": 1530286468000,"productId": 7},{"productImgId": 15,"imgAddr": "\\upload\\item\\shopImage\\5\\2018070100324625530.jpg","imgDesc": null,"priority": 66,"createTime": 1530286468000,"productId": 7}],"productCategory": {"productCategoryId": 36,"shopId": null,"productCategoryName": null,"productCategoryDesc": null,"priority": null,"createTime": null,"lastEditTime": null},"shop": {"shopId": 5,"shopName": null,"shopDesc": null,"shopAddr": null,"phone": null,"shopImg": null,"priority": null,"createTime": null,"lastEditTime": null,"enableStatus": null,"advice": null,"owner": null,"area": null,"shopCategory": null}},"productCategoryList": [{"productCategoryId": 36,"shopId": 5,"productCategoryName": "奶茶","productCategoryDesc": "香飄飄奶茶","priority": 99,"createTime": 1529678397000,"lastEditTime": 1529678397000},{"productCategoryId": 37,"shopId": 5,"productCategoryName": "咖啡","productCategoryDesc": "拿鐵咖啡","priority": 98,"createTime": 1529678397000,"lastEditTime": 1529678397000}],"success": true }檢查數據是否符合預期。
Github地址
代碼地址: https://github.com/yangshangwei/o2o
總結
以上是生活随笔為你收集整理的实战SSM_O2O商铺_34【商品】商品编辑之Controller层的实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 实战SSM_O2O商铺_33【商品】商品
- 下一篇: 实战SSM_O2O商铺_35【商品】商品