最新公告
  • 新注册用户请前往个人中心绑定邮箱以便接收相关凭证邮件!!!点击前往个人中心
  • 从零到企业级SSM电商项目实战教程(二十七)后台获取商品详情功能开发

    ProductManageController.java

    /*
     * @Description: 获取产品详情
     *
     * @auther: Geekerstar(jikewenku.com)
     * @date: 2018/6/23 15:21   
     * @param: [session, productId]
     * @return: com.mmall.common.ServerResponse
     */
    @RequestMapping("detail.do")
    public ServerResponse getDetail(HttpSession session, Integer productId){
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录,请登录管理员");
    
        }
        if(iUserService.checkAdminRole(user).isSuccess()){
            //填充业务
            return iProductService.manageProductDetail(productId);
    
        }else{
            return ServerResponse.createByErrorMessage("无权限操作");
        }
    }
    

    IProductService.java

    ServerResponse manageProductDetail(Integer productId);
    

    ProductServiceImpl.java

    public ServerResponse manageProductDetail(Integer productId){
        if(productId == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(),ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }
        Product product = productMapper.selectByPrimaryKey(productId);
        if(product == null){
            return ServerResponse.createByErrorMessage("产品已下架或者删除");
        }
        //VO对象--value object
        //POJO-BO(business object)-->VO(view object)
        ProductDetailVo productDetailVo = assembleProductDetailVo(product);
        return ServerResponse.createBySuccess(productDetailVo);
    }
    
    private ProductDetailVo assembleProductDetailVo(Product product){
        ProductDetailVo productDetailVo = new ProductDetailVo();
        productDetailVo.setId(product.getId());
        productDetailVo.setSubtitle(product.getSubtitle());
        productDetailVo.setPrice(product.getPrice());
        productDetailVo.setMainImage(product.getMainImage());
        productDetailVo.setSubImages(product.getSubImages());
        productDetailVo.setCategoryId(product.getCategoryId());
        productDetailVo.setDetail(product.getDetail());
        productDetailVo.setName(product.getName());
        productDetailVo.setStatus(product.getStatus());
        productDetailVo.setStock(product.getStock());
    
        productDetailVo.setImageHost(PropertiesUtil.getProperty("ftp.server.http.prefix","http://img.happymmall.com/"));
    
        Category category = categoryMapper.selectByPrimaryKey(product.getCategoryId());
        if(category == null){
            productDetailVo.setParentCategoryId(0);//默认根节点
        }else{
            productDetailVo.setParentCategoryId(category.getParentId());
        }
    
        productDetailVo.setCreateTime(DateTimeUtil.dateToStr(product.getCreateTime()));
        productDetailVo.setUpdateTime(DateTimeUtil.dateToStr(product.getUpdateTime()));
        return productDetailVo;
    }
    

    ProductDetailVo.java

    vo包下新建ProductDetailVo.java

    package com.mmall.vo;
    
    import java.math.BigDecimal;
    
    /**
     * @Author: Geekerstar(jikewenku.com)
     * @Date: 2018/6/23 14:59
     * @Description:
     */
    public class ProductDetailVo {
        private Integer  id;
        private Integer categoryId;
        private String name;
        private String subtitle;
        private String mainImage;
        private String subImages;
        private String detail;
        private BigDecimal price;
        private Integer stock;
        private Integer status;
        private String createTime;
        private String updateTime;
    
        private String imageHost;
        private Integer parentCategoryId;
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public Integer getCategoryId() {
            return categoryId;
        }
    
        public void setCategoryId(Integer categoryId) {
            this.categoryId = categoryId;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getSubtitle() {
            return subtitle;
        }
    
        public void setSubtitle(String subtitle) {
            this.subtitle = subtitle;
        }
    
        public String getMainImage() {
            return mainImage;
        }
    
        public void setMainImage(String mainImage) {
            this.mainImage = mainImage;
        }
    
        public String getSubImages() {
            return subImages;
        }
    
        public void setSubImages(String subImages) {
            this.subImages = subImages;
        }
    
        public String getDetail() {
            return detail;
        }
    
        public void setDetail(String detail) {
            this.detail = detail;
        }
    
        public BigDecimal getPrice() {
            return price;
        }
    
        public void setPrice(BigDecimal price) {
            this.price = price;
        }
    
        public Integer getStock() {
            return stock;
        }
    
        public void setStock(Integer stock) {
            this.stock = stock;
        }
    
        public Integer getStatus() {
            return status;
        }
    
        public void setStatus(Integer status) {
            this.status = status;
        }
    
        public String getCreateTime() {
            return createTime;
        }
    
        public void setCreateTime(String createTime) {
            this.createTime = createTime;
        }
    
        public String getUpdateTime() {
            return updateTime;
        }
    
        public void setUpdateTime(String updateTime) {
            this.updateTime = updateTime;
        }
    
        public String getImageHost() {
            return imageHost;
        }
    
        public void setImageHost(String imageHost) {
            this.imageHost = imageHost;
        }
    
        public Integer getParentCategoryId() {
            return parentCategoryId;
        }
    
        public void setParentCategoryId(Integer parentCategoryId) {
            this.parentCategoryId = parentCategoryId;
        }
    }
    

    PropertiesUtil配置工具

    util包下新建PropertiesUtil.java

    package com.mmall.util;
    
    import org.apache.commons.lang.StringUtils;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.Properties;
    
    /**
     * @Author: Geekerstar(jikewenku.com)
     * @Date: 2018/6/23 15:05
     * @Description:
     */
    public class PropertiesUtil {
        private static Logger logger = LoggerFactory.getLogger(PropertiesUtil.class);
    
        private static Properties props;
    
        static {
            String fileName = "mmall.properties";
            props = new Properties();
            try {
                props.load(new InputStreamReader(PropertiesUtil.class.getClassLoader().getResourceAsStream(fileName),"UTF-8"));
            } catch (IOException e) {
                logger.error("配置文件读取异常",e);
            }
        }
    
        public static String getProperty(String key){
            String value = props.getProperty(key.trim());
            if(StringUtils.isBlank(value)){
                return null;
            }
            return value.trim();
        }
    
        public static String getProperty(String key,String defaultValue){
    
            String value = props.getProperty(key.trim());
            if(StringUtils.isBlank(value)){
                value = defaultValue;
            }
            return value.trim();
        }
    }
    

    DateTimeUtil时间处理工具

    util包下新建DateTimeUtil.java

    package com.mmall.util;
    
    import org.apache.commons.lang3.StringUtils;
    import org.joda.time.DateTime;
    import org.joda.time.format.DateTimeFormat;
    import org.joda.time.format.DateTimeFormatter;
    
    import java.util.Date;
    
    /**
     * @Author: Geekerstar(jikewenku.com)
     * @Date: 2018/6/23 15:13
     * @Description:
     */
    public class DateTimeUtil {
        //joda-time
    
        //str->Date
        //Date->str
        public static final String STANDARD_FORMAT = "yyyy-MM-dd HH:mm:ss";
    
    
    
        public static Date strToDate(String dateTimeStr, String formatStr){
            DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(formatStr);
            DateTime dateTime = dateTimeFormatter.parseDateTime(dateTimeStr);
            return dateTime.toDate();
        }
    
        public static String dateToStr(Date date,String formatStr){
            if(date == null){
                return StringUtils.EMPTY;
            }
            DateTime dateTime = new DateTime(date);
            return dateTime.toString(formatStr);
        }
    
        public static Date strToDate(String dateTimeStr){
            DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(STANDARD_FORMAT);
            DateTime dateTime = dateTimeFormatter.parseDateTime(dateTimeStr);
            return dateTime.toDate();
        }
    
        public static String dateToStr(Date date){
            if(date == null){
                return StringUtils.EMPTY;
            }
            DateTime dateTime = new DateTime(date);
            return dateTime.toString(STANDARD_FORMAT);
        }
    
    
    
    
        public static void main(String[] args) {
            System.out.println(DateTimeUtil.dateToStr(new Date(),"yyyy-MM-dd HH:mm:ss"));
            System.out.println(DateTimeUtil.strToDate("2010-01-01 11:11:11","yyyy-MM-dd HH:mm:ss"));
    
        }
    }
    
    本站所有文章均由网友分享,仅用于参考学习用,请勿直接转载,如有侵权,请联系网站客服删除相关文章。若由于商用引起版权纠纷,一切责任均由使用者承担
    极客文库 » 从零到企业级SSM电商项目实战教程(二十七)后台获取商品详情功能开发

    常见问题FAQ

    如果资源链接失效了怎么办?
    本站用户分享的所有资源都有自动备份机制,如果资源链接失效,请联系本站客服QQ:2580505920更新资源地址。
    如果用户分享的资源与描述不符怎么办?
    可以联系客服QQ:2580505920,如果要求合理可以安排退款或者退赞助积分。
    如何分享个人资源获取赞助积分或其他奖励?
    本站用户可以分享自己的资源,但是必须保证资源没有侵权行为。点击个人中心,根据操作填写并上传即可。资源所获收益完全归属上传者,每周可申请提现一次。
    如果您发现了本资源有侵权行为怎么办?
    及时联系客服QQ:2580505920,核实予以删除。

    参与讨论

    • 192会员总数(位)
    • 3737资源总数(个)
    • 0本周发布(个)
    • 0 今日发布(个)
    • 614稳定运行(天)

    欢迎加入「极客文库」,成为原创作者从这里开始!

    立即加入 了解更多
    成为赞助用户享有更多特权立即升级