com\imooc\controller\SellerProductController.javacss
package com.imooc.controller; import com.imooc.exception.SellException; import com.imooc.form.ProductForm; import com.imooc.service.ProductCategoryService; import com.imooc.service.ProductInfoService; import com.imooc.utils.KeyUtil; import com.imooc.vo.admin.ProductCategory; import com.imooc.vo.admin.ProductInfo; import com.imooc.vo.enums.ProductStatusEnum; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.stereotype.Controller; import org.springframework.util.StringUtils; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.ModelAndView; import javax.validation.Valid; import java.util.List; import java.util.Map; /** * @author: menghaibin * @create: 2020-02-25 20:23 * @description: 賣家端商品 **/ Controller RequestMapping("/seller/product") public class SellerProductController { @Autowired private ProductInfoService productInfoService; @Autowired private ProductCategoryService productCategoryService; }
1:查詢全部商品:html
@GetMapping("/list") public ModelAndView list(@RequestParam(value = "page",defaultValue = "1") Integer page, @RequestParam(value = "size",defaultValue = "2") Integer size, Map<String,Object> map){ PageRequest request = new PageRequest(page-1,size); Page<ProductInfo> productInfoPage = productInfoService.findAll(request); map.put("productInfoPage",productInfoPage); map.put("currentPage",page);/*當前頁,用於前端分頁*/ map.put("size",size);/*每頁多少條*/ return new ModelAndView("product/list",map); }
建立list方法須要返回的list的頁面:
resources\templates\product\list.ftl前端
<html> <head> <meta charset="UTF-8"> <title>賣家商品列表</title> <link href="https://cdn.bootcss.com/twitter-bootstrap/3.0.1/css/bootstrap.min.css" rel="stylesheet"> <#--側邊欄樣式--> <link rel="stylesheet" href="/sell/css/style.css"> </head> <body> <div id="wrapper" class="toggled"> <#--側欄--> <#include "../common/nav.ftl" > <#--主內容--> <div id="page-content-wrapper"> <div class="container-fluid"> <div class="row clearfix"> <div class="col-md-12 column"> <table class="table table-bordered"> <thead> <tr> <th>商品id</th> <th>姓名</th> <th>圖片</th> <th>單價</th> <th>庫存</th> <th>描述</th> <th>類目</th> <th>建立時間</th> <th>修改時間</th> <th colspan="2">操做</th> </tr> </thead> <tbody> <#--遍歷獲取訂單列表--> <#list productInfoPage.getContent() as productInfo> <tr> <td>${productInfo.productId}</td> <td>${productInfo.productName}</td> <td> <img width="80" height="80" src="${productInfo.productIcon}"> </td> <td>${productInfo.productPrice}</td> <td>${productInfo.productStock}</td> <td>${productInfo.productDescription}</td> <td>${productInfo.categoryType}</td> <td>${productInfo.createTime}</td> <td>${productInfo.updateTime}</td> <td> <a href="/sell/seller/product/index?productId=${productInfo.productId}">修改</a> </td> <td> <#if productInfo.productStatus == 0> <a href="/sell/seller/product/off_sale?productId=${productInfo.productId}">下架</a> <#else> <a href="/sell/seller/product/on_sale?productId=${productInfo.productId}">上架</a> </#if> </td> </tr> </#list> </tbody> </table> </div> <#--分頁--> <div class="col-md-12 column"> <ul class="pagination pull-right"> <#--判斷上一頁是否可點擊--> <#if currentPage lte 1> <li class="disabled"><a href="#">上一頁</a></li> <#else> <li><a href="/sell/seller/product/list?page=${currentPage-1}&size=${size}">上一頁</a></li> </#if> <#--獲取總頁數--> <#list 1..productInfoPage.getTotalPages() as index> <#--若是等於當前頁 當前頁的分頁標籤不可點擊--> <#if currentPage == index> <li class="disabled"><a href="#"> ${index}</a></li> <#else> <li><a href="/sell/seller/product/list?page=${index}&size=${size}"> ${index}</a></li> </#if> </#list> <#--判斷下一頁是否可點擊--> <#if currentPage gte productInfoPage.getTotalPages()> <li class="disabled"><a href="#">下一頁</a></li> <#else> <li><a href="/sell/seller/product/list?page=${currentPage+1}&size=${size}">下一頁</a></li> </#if> </ul> </div> </div> </div> </div> </div> </body> </html>
預覽頁面:
http://127.0.0.1/sell/seller/product/list
java
2:商品上架功能:
建立商品狀態枚舉類:
com\imooc\vo\enums\ProductStatusEnum.javaweb
package com.imooc.vo.enums; import lombok.Getter; /** * 商品狀態(枚舉) * Created by Administrator on 2020/2/9. */ @Getter public enum ProductStatusEnum { up(0,"上架"), down(1,"下架"), ; private Integer code; private String message; ProductStatusEnum(Integer code,String message) { this.code = code; this.message = message; } }
在ProductInfoService接口中追加上架和下架的方法:spring
/*上架*/ public ProductInfo on_sale(String ProductId); /*下架*/ public ProductInfo off_sale(String ProductId);
在ProductInfoServiceImpl類中實現上架和下架的方法:bootstrap
/*上架*/ @Override public ProductInfo on_sale(String ProductId) { ProductInfo productInfo = productInfoDao.findOne(ProductId); if(productInfo == null){ throw new SellException(ResultEnum.PRODUCT_NOT_EXIST); } productInfo.setProductStatus(0);/*更新上架狀態*/ return productInfoDao.save(productInfo); } /*下架*/ @Override public ProductInfo off_sale(String ProductId) { ProductInfo productInfo = productInfoDao.findOne(ProductId); if(productInfo == null){ throw new SellException(ResultEnum.PRODUCT_NOT_EXIST); } productInfo.setProductStatus(1);/*更新上架狀態*/ return productInfoDao.save(productInfo); }
在SellerOrderController中追加on_sale上架和off_sale下架的方法app
@RequestMapping("/on_sale") public ModelAndView on_sale(@RequestParam("productId") String productId, Map<String,Object> map){ try{ ProductInfo productInfo = productInfoService.on_sale(productId); }catch(SellException e){ map.put("msg",e.getMessage()); return new ModelAndView("common/error",map); } map.put("url","/sell/seller/product/list"); return new ModelAndView("common/success",map); } @RequestMapping("/off_sale") public ModelAndView off_sale(@RequestParam("productId") String productId, Map<String,Object> map){ try{ ProductInfo productInfo = productInfoService.off_sale(productId); }catch(SellException e){ map.put("msg",e.getMessage()); map.put("url","/sell/seller/product/list"); return new ModelAndView("common/error",map); } map.put("url","/sell/seller/product/list"); return new ModelAndView("common/success",map); }
4:商品新增和修改功能:
在SellerOrderController中追加index跳轉Form表單提交頁面的方法:
判斷商品id是否爲空,若是不爲空就把數據回顯到添加頁面dom
@GetMapping("index") /*productId:非比傳的 以此來判斷是修改仍是新增*/ public ModelAndView index(@RequestParam(value = "productId",required = false) String productId, Map<String,Object> map){ /*若是productId不爲空*/ if(!StringUtils.isEmpty(productId)){ ProductInfo productInfo = productInfoService.findOne(productId); map.put("productInfo",productInfo); } /*查詢全部的類目*/ List<ProductCategory> productCategoryList =productCategoryService.findAll(); map.put("productCategoryList",productCategoryList); return new ModelAndView("product/index",map); }
建立新增頁面:
resources\templates\product\index.ftlide
<html> <head> <meta charset="UTF-8"> <title>賣家商品列表</title> <link href="https://cdn.bootcss.com/twitter-bootstrap/3.0.1/css/bootstrap.min.css" rel="stylesheet"> <#--側邊欄樣式--> <link rel="stylesheet" href="/sell/css/style.css"> </head> <body> <div id="wrapper" class="toggled"> <#--側欄--> <#include "../common/nav.ftl" > <#--主內容--> <div id="page-content-wrapper"> <div class="container-fluid"> <div class="row clearfix"> <div class="col-md-12 column"> <#--表單提交--> <form role="form" action="/sell/seller/product/save" method="post"> <div class="form-group"> <label>名稱</label> <input type="text" class="form-control" name="productName" value="${(productInfo.productName)!""}"/> </div> <div class="form-group"> <label>價格</label> <input type="text" class="form-control" name="productPrice" value="${(productInfo.productPrice)!""}"/> </div> <div class="form-group"> <label>庫存</label> <input type="number" class="form-control" name="productStock" value="${(productInfo.productStock)!""}"/> </div> <div class="form-group"> <label>描述</label> <input type="text" class="form-control" name="productDescription" value="${(productInfo.productDescription)!""}"/> </div> <div class="form-group"> <label>圖片</label> <img src="${(productInfo.productIcon)!""}" width="150" height="150"> <input type="text" class="form-control" name="productIcon" value="${(productInfo.productIcon)!""}"/> </div> <div class="form-group"> <label>類目</label> <select name="categoryType" class="form-control" > <#list productCategoryList as productCategory> <option value="${productCategory.categoryType}" <#--??:判斷是否存在--> <#if (productInfo.categoryType)?? && productInfo.categoryType==productCategory.categoryType> selected </#if> > ${productCategory.categoryName} </option> </#list> </select> </div> <#--隱藏域 產品id--> <input type="hidden" name="productId" value="${(productInfo.productId)!""}"> <#--隱藏域 產品狀態--> <input type="hidden" name="productStatus" value="${(productInfo.productStatus)!""}"> <button type="submit" class="btn btn-default">提交</button> </form> </div> </div> </div> </div> </div> </body> </html>
新增或者修改商品表單提交:
建立商品表單提交字段的form javabena
com\imooc\form\ProductForm.java
package com.imooc.form; import lombok.Data; import java.math.BigDecimal; /** * @author: menghaibin * @create: 2020-02-26 12:30 * @description: 商品提交表單 **/ @Data public class ProductForm { private String productId; /*商品名稱*/ private String productName; /*商品單價*/ private BigDecimal productPrice; /*商品庫存*/ private Integer productStock; /*商品描述*/ private String productDescription; /*商品小圖路徑*/ private String productIcon; /*商品狀態 0正常 1下架*/ private Integer productStatus; /*類目編號*/ private Integer categoryType; }
在SellerOrderController中追加save商品提交方法:
@PostMapping("save") public ModelAndView save(@Valid ProductForm productForm, BindingResult bindingResult, Map<String,Object> map){ if(bindingResult.hasErrors()){ map.put("msg",bindingResult.getFieldError().getDefaultMessage()); map.put("url","/sell/seller/product/index"); return new ModelAndView("common/error",map); } ProductInfo productInfo = new ProductInfo(); /*判斷是修改仍是新增的賦值*/ if(!StringUtils.isEmpty(productForm.getProductId())){ productInfo.setProductId(productForm.getProductId()); productInfo.setProductStatus(productForm.getProductStatus()); }else { productInfo.setProductId(KeyUtil.genUniqueKey()); productInfo.setProductStatus(ProductStatusEnum.up.getCode()); } productInfo.setProductName(productForm.getProductName()); productInfo.setProductPrice(productForm.getProductPrice()); productInfo.setProductIcon(productForm.getProductIcon()); productInfo.setProductDescription(productForm.getProductDescription()); productInfo.setCategoryType(productForm.getCategoryType()); productInfo.setProductStock(productForm.getProductStock()); try{ productInfoService.save(productInfo); }catch (SellException e){ map.put("msg",e.getMessage()); map.put("url","/sell/seller/product/index"); return new ModelAndView("common/error",map); } map.put("url","/sell/seller/product/list"); return new ModelAndView("common/success",map); }
預覽頁面: