如何将数据从 angularjs 传递到 spring 控制器
how to pass the data from angularjs to spring controller
Angularjs代码
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
说明服务器遇到意外情况,无法完成请求。
您应该将用户名传递给 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;
});
};
}]);
Angularjs代码
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 说明服务器遇到意外情况,无法完成请求。
您应该将用户名传递给 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;
});
};
}]);