增加品牌 4.1 需求分析 实现品牌增加功能 4.1 后端代码4.1.1 服务接口层 在 pinyougou-sellergoods-interface 的 BrandService.java 新增方法定义

/**
 
* 增 加
 
*/
 
public  void  add(TbBrand  brand);
4.1.1 服务实现层
在 com.pinyougou.sellergoods.service.impl 的 BrandServiceImpl.java 实现该方法
@Override
 
public  void  add(TbBrand  brand)  { brandMapper.insert(brand);
}

4.1.1 执行结果封装实体 在 pinyougou-pojo 的 entity 包下创建类 Result.java

package  entity;
 
 
import  java.io.Serializable;
 
 
/**
 
*返回结果封装
 
*@author  Administrator
 
 
*
 
 
*/
 
 
public  class  Result  implements  Serializable{
 
 
private  boolean  success;
 
 
private  String  message;
 
 
public  Result(boolean  success,  String  message)  {
 
 
super();
 
 
this.success  =  success;
 
 
this.message  =  message;
 
 
}
 
 
//getter  and  setter.....
 
 
}

4.1.1 控制层 在 pinyougou-manager-web 的 BrandController.java 中新增方法

/**

*增加
 
*@param  brand
 
*@return
 
*/ @RequestMapping("/add")
public  Result  add(@RequestBody  TbBrand  brand){
 
try {
 
brandService.add(brand);
 
return  new  Result(true,  "增加成功");
 
}  catch  (Exception  e)  { e.printStackTrace();
return  new  Result(false,  "增加失败");
 
}
 
}

4.1 前端代码 4.1.1 JS代码

//保存
 
$scope.save=function(){
 
$http.post('../brand/add.do',$scope.entity  ).success(
function(response){ if(response.success){
//重新查询
 
$scope.reloadList();//重新加载
 
}else{
 
alert(response.message);
 
}
 
}
 
);
 
}

4.1.1 HTML 绑定表单元素,我们用 ng-model 指令,绑定按钮的单击事件我们用 ng-click

<div  class="modal-content">
 
 
<div  class="modal-header">
 
 
<button  type="button"  class="close"  data-dismiss="modal" aria-hidden="true">×</button>
<h3  id="myModalLabel">品牌编辑</h3>
 
</div>
 
 
<div  class="modal-body">
 
 
<table  class="table  table-bordered  table-striped"    width="800px">
 
 
<tr>
 
<td>品牌名称</td>
 
<td><input    class="form-control"  ng-model="entity.name"
placeholder="品牌名称"  >    </td>
 
</tr>
<tr>
 
<td>首字母</td>
 
<td><input    class="form-control"  ng-model="entity.firstChar" placeholder="首字母">    </td>
 
</tr>
 
 
</table>
 
 
</div>
 
 
<div  class="modal-footer">
 
 
<button  class="btn  btn-success"  data-dismiss="modal"  aria-hidden="true" ng-click="save()">保存</button>
 
<button  class="btn  btn-default"  data-dismiss="modal"  aria-hidden="true">
关闭</button>
 
</div>
 
 
</div>
 
</div>

为了每次打开窗口没有遗留上次的数据,我们可以修改新建按钮,对 entity 变量进行清空操作

<button  type="button"  class="btn  btn-default"  title="新建"  data-toggle="modal"
data-target="#editModal"  ng-click="entity={}"><i  class="fa  fa-file-o"></i>  新建
</button>