I have one simple web application in JAVA and angularjs. Where users can add persons to app and remove them from mongo database.
My problem is, I don't know exactly how angular communicates with java and calls Java functions. For example if i want to delete a person from my database after a button click.
here's some code
persons.html
<a for-authenticated ng-click="remove(s.id)" href=""> <i
class="pull-right glyphicon glyphicon-remove"></i>
</a>
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;
}) : {};
};
}
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);
}
}
PersonResource.js
app.factory('PersonResource', function ($resource) {
return $resource('rest/person/:personId',
{
personId: '@personId'
},
{
'update': { method: 'PUT' }
})
});
also i have a java class where i want to delete this person from database
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);
}
}
PersonRepository
@Override
public void deleteObject(String id) {
getTemplate().remove(new Query(Criteria.where("id").is(id)), Person.class);
}
the getTemplate() returns MongoTemplate.
Can anyone tell me what i am doing wrong to get my entry deleted from database ?
There are so many ways we can build Angular apps and ship for production. One way is to build Angular with Java. In the development phase, we can run Angular and Java on separate ports. The interaction between these two happens with proxying all the calls to API.
Angular is an open-source, JavaScript framework written in TypeScript. Google maintains it, and its primary purpose is to develop single-page applications.
So when using GET method usual we fetch some data, if we want to send some data to server (ex. an id for person to be deleted) we use POST method or DELETE method, in my example I'll use POST method for simplification. Angular and java communicate thru RESTFUL services (JAX-RS), You cant call java function in angular js or vice verse. I'll show simple example of fetching data and sending data (fetch all persons, delete person with given id).
Here is an example where You can start learning from:
Java Person Controller
@Controller
@RequestMapping(value = "/person")
public class PersonController{
private final PersonService personService;
@Autowired
public PersonController(final PersonService personService) {
this.personService= personService;
}
@RequestMapping(value = "/", method = { RequestMethod.GET })
@ResponseBody
public List<Person> getPersons() {
return personService.getPersons();
}
@RequestMapping(value = "/delete/{personId}", method = { RequestMethod.POST})
@ResponseBody
public HttpStatus deletePerson(@PathVariable final long personId) {
return personService.deletePerson(personId);
}
}
Java Person Service
public class PersonService{
private final PersonRepository personRepository;
@Autowired
public PersonService(final PersonRepository personRepository) {
this.personRepository= personRepository;
}
public List<Person> getPersons() {
return personRepository.findAll();;
}
public HttpStatus deletePerson(final long id) {
if (id > 0) {
personRepository.delete(id);
return HttpStatus.OK;
} else {
return HttpStatus.INTERNAL_SERVER_ERROR;
}
}
}
Java Person Repository
public interface PersonRepository{
public void delete(int personId);
public List<Person> findAll();
}
Angular app.js
(function() {
var app = angular.module('personApp',[]);
app.controller('personCtrl',function($scope,$http){
$scope.getPersons = function(){
$http.get('person/').success(function(response){
$scope.allPersons = response.data;
}).error(function(){
//handle error
})
};
$scope.deletePerson = function(personId){
$http.delete('person/'+personId).success(function(response){
$scope.deleteResponseStatus = response.status;
}).error(function(){
//handle error
})
}
})
})();
Html
<html ng-app="personApp">
<body ng-controller=""personCtr>
<input type="submit" ng-click="getPersons()"/>
<input type="submit" ng-click="deletePerson(somePersonIdFromTableOrSomething)"
</body>
</html>
Hope it will help, not tested but generally that is the flow..send request to controller with person id, find it in database and delete it
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With