Angularjs调用java函数

use*_*600 6 javascript java mongodb angularjs

我在JAVA和angularjs中有一个简单的Web应用程序.用户可以将人员添加到应用程序并从mongo数据库中删除.

我的问题是,我不确切知道如何与java通信并调用Java函数.例如,如果我想在按钮单击后从我的数据库中删除一个人.

这是一些代码

persons.html

<a for-authenticated ng-click="remove(s.id)" href=""> <i
     class="pull-right glyphicon glyphicon-remove"></i>
</a>
Run Code Online (Sandbox Code Playgroud)

app.js

var app = angular.module('conferenceApplication', [
'ngCookies',
'ngResource',
'ngSanitize',
'ngRoute',
'ui.bootstrap',
'angularFileUpload',
'ngQuickDate']);


 app.config(function ($routeProvider) {
    $routeProvider
      .when('/home', {
           templateUrl: '/partials/home.html',
           controller: 'HomeCtrl'
      })
      .when('/speakers', {
          templateUrl: '/partials/person-list.html',
          controller: 'PersonListCtrl'
      })
});
app.controller('PersonListCtrl', function ($scope,$http, $modal, $log, $route, PersonService) {
$scope.remove = function(id) {
    var deletedPerson = id ? PersonService.remove(id, function(resp){
        deletedPerson = resp;
    }) : {};
};
}
Run Code Online (Sandbox Code Playgroud)

PersonService.js

app.service('PersonService', function ($log, $upload, PersonResource) {
this.getById = function (id, callback) {
    return PersonResource.get({personId: id}, callback);
};
this.remove = function(id, callback) {
    return PersonResource.deleteObject({PersonId: id}, callback);
}
}
Run Code Online (Sandbox Code Playgroud)

PersonResource.js

app.factory('PersonResource', function ($resource) {
return $resource('rest/person/:personId',
    {
        personId: '@personId'
    },
    {
        'update': { method: 'PUT' }
    })
Run Code Online (Sandbox Code Playgroud)

});

我也有一个java类,我想从数据库中删除此人

PersonResource.java

@Controller
 @RequestMapping("/person")
   public class PersonResource {

     @Autowired
     private PersonService personService;

     @RequestMapping(method = RequestMethod.GET, value = "/{id}")
     public ResponseEntity<Person> deleteObject(@RequestBody Person id) {
        Person person = personService.findById(id);
        personService.deleteObject(id);
        return new ResponseEntity<Person>(person, HttpStatus.ACCEPTED);
     }
   }
Run Code Online (Sandbox Code Playgroud)

PersonRepository

  @Override
  public void deleteObject(String id) {
      getTemplate().remove(new Query(Criteria.where("id").is(id)), Person.class);
  }
Run Code Online (Sandbox Code Playgroud)

getTemplate()返回MongoTemplate.

任何人都可以告诉我,我的错误是从数据库中删除我的条目吗?

小智 0

您需要单独解决这些问题。尝试单独测试您的休息服务。使用 POSTMAN 或任何其他 REST 客户端 ( https://addons.mozilla.org/en-US/firefox/addon/restclient/ ) 并测试服务是否有相应的行为。仅当成功时,才检查 Javascript 客户端(AngularJS 代码)并查看请求(标头、参数和正文)是否与 REST 客户端生成的请求相同。