如何将数据从angularjs传递给spring控制器
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何将数据从angularjs传递给spring控制器相关的知识,希望对你有一定的参考价值。
Angular 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";
}
实际上,它的用户名不能从角度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传递给spring控制器的主要内容,如果未能解决你的问题,请参考以下文章
如何将数据 Codeigniter 控制器传递给 Angularjs 控制器