实战SSM_O2O商铺_30【商品】商品添加之Controller层的实现
生活随笔
收集整理的這篇文章主要介紹了
实战SSM_O2O商铺_30【商品】商品添加之Controller层的实现
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
文章目錄
- 概述
- ProductController
- 單元測試
- Github地址
概述
商品添加Controller層的邏輯如下:
1. 獲取前端傳遞過來的Product對象,通過FastJson提供的api將其轉(zhuǎn)換為Product對象
2. 獲取前端傳遞過來的商品縮略圖以及商品詳情圖片,通過CommonsMultipartResolver來處理
3. 調(diào)用Service層的服務(wù)來持久化數(shù)據(jù)及圖片的操作
ProductController
package com.artisan.o2o.web.shopadmin;import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;import javax.servlet.http.HttpServletRequest;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartHttpServletRequest; import org.springframework.web.multipart.commons.CommonsMultipartFile; import org.springframework.web.multipart.commons.CommonsMultipartResolver;import com.artisan.o2o.dto.ImageHolder; import com.artisan.o2o.dto.ProductExecution; import com.artisan.o2o.entity.Product; import com.artisan.o2o.entity.Shop; import com.artisan.o2o.enums.ProductStateEnum; import com.artisan.o2o.exception.ProductOperationException; import com.artisan.o2o.service.ProductService; import com.artisan.o2o.util.HttPServletRequestUtil; import com.artisan.o2o.util.VerifyCodeUtil; import com.fasterxml.jackson.databind.ObjectMapper;@Controller @RequestMapping("/shopadmin") public class ProductController {@Autowiredprivate ProductService productService;// 最大上傳圖片數(shù)量private static final int IMAGEMAXCOUNT = 6;/*** * * @Title: addProduct* * @Description: 1. 驗證碼校驗* * 2. 接收前端參數(shù):包括 商品、 商品縮略圖、商品詳情圖片實體類* * 前端頁面通過post方式傳遞一個包含文件上傳的Form會以multipart/form-data請求發(fā)送給服務(wù)器,* * 需要告訴DispatcherServlet如何處理MultipartRequest,我們在spring-web.* xml中定義了multipartResolver。* * 如果某個Request是一個MultipartRequest,它就會首先被MultipartResolver處理,* 然后再轉(zhuǎn)發(fā)相應(yīng)的Controller。* * 在Controller中,* 將HttpServletRequest轉(zhuǎn)型為MultipartHttpServletRequest* ,可以非常方便的得到文件名和文件內(nèi)容* * @param request* * @return: Map<String,Object>* * 注解@ResponseBody 負(fù)責(zé)將返回的map對象轉(zhuǎn)換為JSON,供前端使用*/@RequestMapping(value = "/addproduct", method = RequestMethod.POST)@ResponseBodypublic Map<String, Object> addProduct(HttpServletRequest request) {Map<String, Object> modelMap = new HashMap<String, Object>();Product product = null;// 接收前端傳遞過來的productString productStr = null;// 商品圖片縮略圖(輸入流和名稱的封裝類)ImageHolder thumbnail = null;// 將HttpServletRequest轉(zhuǎn)型為MultipartHttpServletRequest,可以很方便地得到文件名和文件內(nèi)容MultipartHttpServletRequest multipartHttpServletRequest = null;// 接收商品縮略圖CommonsMultipartFile thumbnailFile = null;// 接收商品詳情圖片List<ImageHolder> productDetailImgList = new ArrayList<ImageHolder>();// 創(chuàng)建一個通用的多部分解析器CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());// Step1:校驗驗證碼if (!VerifyCodeUtil.verifyCode(request)) {modelMap.put("success", false);modelMap.put("errMsg", "驗證碼不正確");return modelMap;}// Step2: 使用FastJson提供的api,實例化Product 構(gòu)造調(diào)用service層的第一個參數(shù)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: 商品縮略圖 和 商品詳情圖 構(gòu)造調(diào)用service層的第二個參數(shù)和第三個參數(shù)try {// 判斷 request 是否有文件上傳,即多部分請求if (commonsMultipartResolver.isMultipart(request)) {// 將request轉(zhuǎn)換成多部分requestmultipartHttpServletRequest = (MultipartHttpServletRequest) request;// 得到縮略圖的CommonsMultipartFile ,和前端約定好使用thumbnail 傳遞// ,并構(gòu)建ImageHolder對象thumbnailFile = (CommonsMultipartFile) multipartHttpServletRequest.getFile("thumbnail");if(thumbnailFile == null){modelMap.put("success", false);modelMap.put("errMsg", "上傳圖片不能為空~");return modelMap;}// 轉(zhuǎn)化為ImageHolder,使用service層的參數(shù)類型要求thumbnail = new ImageHolder(thumbnailFile.getInputStream() ,thumbnailFile.getOriginalFilename());// 得到 商品詳情的列表,和前端約定使用productImg + i 傳遞 ,并構(gòu)建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為空,終止循環(huán)break;}}} else {modelMap.put("success", false);modelMap.put("errMsg", "上傳圖片不能為空");return modelMap;}} catch (Exception e) {modelMap.put("success", false);modelMap.put("errMsg", e.toString());return modelMap;}// Step4 調(diào)用Service層if (product != null && thumbnailFile != null && productDetailImgList.size() > 0) {try {// 從session中獲取shop信息,不依賴前端的傳遞更加安全Shop currentShop = (Shop) request.getSession().getAttribute("currentShop");product.setShop(currentShop);// 調(diào)用addProductProductExecution pe = productService.addProduct(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;}}單元測試
待前端頁面開發(fā)完畢后,一并測試
Github地址
代碼地址: https://github.com/yangshangwei/o2o
總結(jié)
以上是生活随笔為你收集整理的实战SSM_O2O商铺_30【商品】商品添加之Controller层的实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 实战SSM_O2O商铺_29【商品】商品
- 下一篇: 实战SSM_O2O商铺_31【商品】商品