在尝试使用Spring MVC Angle JS保存数据时将空值存储在数据库中

时间:2018-11-12 05:12:25

标签: mysql angularjs spring spring-mvc

角度js代码

 <body data-ng-app="myApp" data-ng-controller="UserController as userBean">
<form method="post" action="register" name="myForm">
<div class="form-group col-lg-7" >
  <label for="username" class="control-label">First Name:</label>
  <input type="text" data-ng-model="userBean.username" class="form-control"  placeholder="Enter Firstname"/><br>
   <label for="phone" class="control-label">Phone:</label>
  <input type="text" data-ng-model="userBean.phone" class="form-control"  placeholder="Enter phone no."/><br>
  <label for="email" class="control-label">Email:</label>
  <input type="text" data-ng-model="userBean.email" class="form-control"  placeholder="Enter email"/><br>
  <label for="address" class="control-label">Address:</label>
  <input type="text" data-ng-model="userBean.address" class="form-control"  placeholder="Enter address"/><br>
  <label for="password" class="control-label">Password:</label>
  <input type="password" data-ng-model="userBean.password" class="form-control"  placeholder="Enter password"/><br>
</div>
<div class="form-group col-lg-7">
   <button type="submit" data-ng-click="insertData()" class="btn btn-primary">Submit</button>
</div>
</form>
<script type="text/javascript">
var app = angular.module('myApp', []);
app.controller("UserController", ['$scope', '$http', function($scope, $http, httpPostService) {
 var self=this;

$scope.insertData = function()
{
    alert($scope.userBean.username, $scope.userBean.phone, $scope.userBean.email);
     $http({
         method: "POST",
         url: "register",
         data:{
         username: $scope.userBean.username,
         phone: $scope.userBean.phone,
         email: $scope.userBean.email,
         address: $scope.userBean.address,
         password: $scope.userBean.password}
        }).then(function(response){
         console.log(response.status);
         console.log("in success");

     }, function(response){
         console.log(response.status);
         console.log("in fail");     
     });
};
}]);
</script>

控制器代码

@RequestMapping(value="/register", method = RequestMethod.POST, consumes="application/json")
    public @ResponseBody ModelAndView doRegister(@ModelAttribute @RequestBody UserBean userBean, BindingResult result)
    {

        if(!result.hasFieldErrors())
        {
            if(retrieveService.insert(userBean) != null)
            {
                System.out.println("done");

            }

        }   
        return new ModelAndView("redirect:/welcome");

    }
}

我认为控制器有问题。 userBean具有空值,以将其传递给控制器​​。好心人帮助我 错误也来了

  

HTTP状态415 –不支持的媒体类型原始服务器拒绝为请求提供服务,因为有效负载的格式不是   此方法对目标资源的支持。

2 个答案:

答案 0 :(得分:0)

在您的请求标头中设置内容类型JSON,如下所示。

$http({
         method: "POST",
         url: "register",
         data:{
         username: $scope.userBean.username,
         phone: $scope.userBean.phone,
         email: $scope.userBean.email,
         address: $scope.userBean.address,
         password: $scope.userBean.password},
         headers: {'Content-Type': 'application/json'}
        })

答案 1 :(得分:0)

在可以具有空值的字段上使用@JsonIgnore。