• angular js 页面添加数据保存数据库


    一、编写实体类Controller层返回数据使用

    package entity;

    import java.io.Serializable;

    public class Result implements Serializable{

    private static final long serialVersionUID = -8946453797496982517L;

    private boolean success;
    private String message;
    public Result(boolean success, String message) {
    super();
    this.success = success;
    this.message = message;
    }


    public boolean isSuccess() {
    return success;
    }
    public void setSuccess(boolean success) {
    this.success = success;
    }
    public String getMessage() {
    return message;
    }
    public void setMessage(String message) {
    this.message = message;
    }


    }
    二、编写service
    //添加
    public void save(Brand brand);

    三、编写serviceImpl
    @Override
    public void save(Brand brand) {
    brandDao.insertSelective(brand);
    }
    四、编写controller
    //添加
    @RequestMapping("/save")
    public Result save(@RequestBody Brand brand){
    try {
    brandService.save(brand);
    return new Result(true,"添加成功");
    }catch (Exception e){
    e.printStackTrace();
    return new Result(false,"添加失败");
    }
    }
    五、编写页面html
    //添加保存
    $scope.save=function () {
    var url="../brand/save.do";
    //判断是添加还是修改,添加$scope.entity.id==null,否则执行修改
    if ($scope.entity.id!=null){
    url="../brand/update.do"
    }
    //发送请求$http.post(url,$scope.entity),第一个参数是请求地址,第二个参数是提交的数据
    $http.post(url,$scope.entity).success(function (response) {
    if(response.success){
    //重新加载
    return $scope.reloadList();
    }else {
    alert(response.message);
    }
    });
    }
    //ng-model="entity.name",封装到对象,才可以进行保存:name=>>entity=>>$scope=>>调save()方法存入数据库
    <tr>
    <td>品牌名称</td>
    <td><input class="form-control" placeholder="品牌名称" ng-model="entity.name" > </td>
    </tr>
    <tr>
    <td>首字母</td>
    <td><input class="form-control" placeholder="首字母" ng-model="entity.firstChar" > </td>
    </tr>
    //ng-click="entity={}"点击新建清空缓存,新建页面数据栏为空,不给空值有缓存数据
    <button ng-click="entity={}" type="button" class="btn btn-default" title="新建"
    data-toggle="modal" data-target="#editModal" ><i class="fa fa-file-o"></i> 新建</button>
    <button class="btn btn-success" data-dismiss="modal" aria-hidden="true" ng-click="save()">保存</button>
  • 相关阅读:
    logback-spring.xml配置文件详解
    SpringBoot-Controller接收参数的几种常用方式
    spring boot配置定时任务设置
    SpringCloud 配置文件 application.yml和 bootstrap.yml区别
    ajax/get请求
    ajax封装2
    ajax封装1
    楼层特效
    旋转动画
    联动动画
  • 原文地址:https://www.cnblogs.com/zhangrongfei/p/11332060.html
Copyright © 2020-2023  润新知