1.书写js代码
<div class="modal-footer">
<button class="btn btn-primary addProductType" onclick="addProductType()">添加</button>
<button class="btn btn-primary cancel" data-dismiss="modal">取消</button>
</div>
// 添加商品类型
function addProductType(){
$.post(
'${pageContext.request.contextPath}/backend/productType/add',
{'name':$('#productTypeName').val()},
function(result){ //从后端传来的ResponseResult对象
if(result.status==1){
layer.msg(result.message,{
time:2000,
skin:'successMsg'
},function(){
location.href='${pageContext.request.contextPath}/backend/productType/findAll?pageNum='+${pageInfo.pageNum};
});
}else{
layer.msg(result.message,{
time:2000,
skin:'errorMsg'
});
}
}
);
}
2.书写Controller层
@RequestMapping("/add")
@ResponseBody
public ResponseResult add(String name){
ResponseResult result = new ResponseResult(); //响应数据的封装对象
try {
productTypeService.add(name);
result.setStatus(ResponseStatusConstant.RESPONSE_STATUS_SUCCESS);
result.setMessage("添加成功");
} catch (ProductTypeExistException e) { //自定义异常
//e.printStackTrace();
result.setStatus(ResponseStatusConstant.RESPONSE_STATUS_FAIL);
result.setMessage(e.getMessage());
}
return result;
}
ResponseResult.java
public class ResponseResult {
//状态码
private int status;
//消息
private String message;
//返回数据
private Object data;
public ResponseResult() {
}
public ResponseResult(int status, String message, Object data) {
this.status = status;
this.message = message;
this.data = data;
}
//成功
public static ResponseResult success(Object data){
return new ResponseResult(ResponseStatusConstant.RESPONSE_STATUS_SUCCESS,"success",data);
}
public static ResponseResult success(String message){
return new ResponseResult(ResponseStatusConstant.RESPONSE_STATUS_SUCCESS,message,null);
}
public static ResponseResult success(){
return new ResponseResult(ResponseStatusConstant.RESPONSE_STATUS_SUCCESS,"success",null);
}
//失败
public static ResponseResult fail(){
return new ResponseResult(ResponseStatusConstant.RESPONSE_STATUS_FAIL,"fail",null);
}
public static ResponseResult fail(Object data){
return new ResponseResult(ResponseStatusConstant.RESPONSE_STATUS_FAIL,"fail",data);
}
public static ResponseResult fail(String message){
return new ResponseResult(ResponseStatusConstant.RESPONSE_STATUS_FAIL,message,null);
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
}
3.Service层
@Override
public void add(String name) throws ProductTypeExistException {
ProductType productType = productTypeDao.selectByName(name);
if(null!=productType){
throw new ProductTypeExistException("商品类型已存在");
}
productTypeDao.insert(name, ProductTypeConstant.Product_TYPE_ENABLE);
}
4.Dao层
<select id="selectByName" resultType="ProductType">
select <include refid="productTypeColumn"/>
from t_product_type
where name=#{name}
</select>