微信点餐之后台类目管理(10)

创建controller控制层

com\imooc\controller\SellCategoryController.java

package com.imooc.controller;

import com.imooc.form.CategoryForm;
import com.imooc.service.ProductCategoryService;
import com.imooc.vo.admin.ProductCategory;
import com.imooc.vo.enums.ResultEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
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-26 14:08
 * @description: 卖家类目管理
 **/
Controller
RequestMapping("/seller/category")
@Slf4j
public class SellCategoryController {

    @Autowired
    private ProductCategoryService productCategoryService;

}

1:查询所有类目:

@GetMapping("/list")
public ModelAndView list(Map<String,Object> map){

    List<ProductCategory> productCategoryList = productCategoryService.findAll();
    map.put("productCategoryList",productCategoryList);
    return new ModelAndView("category/list",map);

}

创建list方法需要返回的list的页面:
resources\templates\category\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>type</th>
                            <th>创建时间</th>
                            <th>修改时间</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        <#--遍历获取订单列表-->
                        <#list productCategoryList as category>
                        <tr>
                            <td>${category.categoryId}</td>
                            <td>${category.categoryName}</td>
                            <td>${category.categoryType}</td>
                            <td>${category.createTime}</td>
                            <td> ${category.updateTime}</td>
                            <td>
                                <a href="/sell/seller/category/index?categoryId=${category.categoryId}">修改</a>
                            </td>
                        </tr>
                        </#list>

                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>

</div>

    </body>
</html>

2:类目新增和修改功能:
在SellerOrderController中追加index跳转Form表单提交页面的方法:
判断商品id是否为空,如果不为空就把数据回显到添加页面

@GetMapping("index")
/*categoryId:非比传的 以此来判断是修改还是新增*/
public ModelAndView index(@RequestParam(value = "categoryId",required = false) Integer categoryId,
                          Map<String,Object> map){
    ProductCategory productCategory = new ProductCategory();
    if(categoryId!=null){
        productCategory = productCategoryService.findOne(categoryId);
        if(productCategory == null){
            map.put("msg",ResultEnum.CATEGORY_NOT_EXIST.getMsg());
            map.put("url","/sell/seller/category/list");
            return new ModelAndView("common/error",map);
        }
    }

    map.put("productCategory",productCategory);
    return new ModelAndView("category/index",map);
}

创建新增页面:
resources\templates\category\index.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">
                    <form role="form" action="/sell/seller/category/save" method="post">

                        <div class="form-group">
                            <label>名称</label>
                            <input type="text" class="form-control" name="categoryName" value="${(productCategory.categoryName)!""}"/>
                        </div>

                        <div class="form-group">
                            <label>type</label>
                            <input type="text" class="form-control" name="categoryType" value="${(productCategory.categoryType)!""}"/>
                        </div>


                        <input type="hidden" name="categoryId" value="${(productCategory.categoryId)!""}">
                        <button type="submit" class="btn btn-default">提交</button>

                    </form>
                </div>
            </div>
        </div>
    </div>

</div>

</body>
</html>

新增或者修改商品表单提交:
创建商品表单提交字段的form javabena
com\imooc\form\CategoryForm.java

package com.imooc.form;

import lombok.Data;

/**
 * @author: menghaibin
 * @create: 2020-02-26 14:43
 * @description: 类目表单提交
 **/
@Data
public class CategoryForm {

    private Integer categoryId;

    /*类目名称*/
    private String categoryName;

    /*类目编号*/
    private Integer categoryType;
}

在SellCategoryController中追加save商品提交方法:

@RequestMapping("/save")
public ModelAndView save(@Valid CategoryForm categoryForm,
                         BindingResult bindingResult,
                         Map<String,Object> map){
    if(bindingResult.hasErrors()){
        map.put("msg",bindingResult.getFieldError().getDefaultMessage());
        map.put("url","/sell/seller/category/index");
        return new ModelAndView("common/error",map);
    }
    /*判断是新增还是修改*/
    ProductCategory productCategory = new ProductCategory();
    if (categoryForm.getCategoryId()!=null){
        productCategory.setCategoryId(categoryForm.getCategoryId());
    }
    productCategory.setCategoryName(categoryForm.getCategoryName());
    productCategory.setCategoryType(categoryForm.getCategoryType());
    productCategoryService.save(productCategory);

    map.put("url","/sell/seller/category/list");
    return new ModelAndView("common/success",map);
}
posted @ 2020-04-01 15:30  努力的校长  阅读(231)  评论(0编辑  收藏  举报