如何将数据从angularjs传递到spring控制器

时间:2018-12-12 07:13:56

标签: angularjs spring spring-mvc delete-row angularjs-http

角度js代码

var app = angular.module('myApp', ['ngResource']);
 app.controller('UserController', ['$scope', '$resource', '$http',function($scope,$resource,$http) 
    {
 $scope.deleteRec = function()
         {
            $http({
                method: 'DELETE',
                url: 'delete/:username',
                data: {username: $scope.myform.username},
                headers: {'Content-type': 'application/json;charset=utf-8'}
                }).then(function(response){
                    $scope.Messages = response;
                });
        };      
 }]);
<table border="1" width="50%" height="50%"> 
    <tr><th>user_name</th><th>phone</th><th>email</th><th>delete</th></tr>
     <tr data-ng-repeat="user in usernames">
     <td><span data-ng-bind="user.username"></span></td>
      <td><span data-ng-bind="user.phone"></span></td>
       <td><span data-ng-bind="user.email"></span></td>
        <td><button data-ng-click="deleteRec()">delete</button>
       </tr>   
   </table> 

控制器代码

@RequestMapping(value="/delete/{username}")
    @ResponseBody
    public String delete(@PathVariable String username) 
    {
        System.out.println("delete1");
        String user=retrievedataservice.delete(username);
        System.out.println("delete2");
        return "delete successfully";
    }

实际上,它的用户名不能从angular js传递到spring Controller。错误来像这样 请求处理失败;嵌套的异常是java.lang.NullPointerException 说明服务器遇到意外状况,阻止其满足请求。

1 个答案:

答案 0 :(得分:0)

您应该将用户名传递到deleteRec()方法中。因为您使用的是ng-bind,所以它是数据绑定的一种方式。

<table border="1" width="50%" height="50%"> 
  <tr data-ng-repeat="user in usernames">
    <td><span data-ng-bind="user.username"></span></td>
    <td><button data-ng-click="deleteRec(user.username)">delete</button>
  </tr>   
</table> 

$scope.deleteRec = function(username)
     {
        $http({
            method: 'DELETE',
            url: 'delete/:username',
            data: {username: username},
            headers: {'Content-type': 'application/json;charset=utf-8'}
            }).then(function(response){
                $scope.Messages = response;
            });
    };      
}]);