I am pretty new to AngularJS, I am trying to figure out how I can call a method from my SPRING controller, or is there another way to achieve this?
My app.js looks like this:
// Define a module
var myApp = angular.module('createWorkshopApp', []);
// Create controller
myApp.controller('mainController',
function ($scope, $http) {
$scope.workshop = {};
$scope.createWorkshop = function () {
if ($scope.workshopName == "" || $scope.workshopDescription == "") {
alert("Insufficient Data! Please provide values for workshop name and description");
}
else {
$http({
method: 'POST',
url: '/create',
data: $scope.workshop
}).
success(function (data, status, headers, config) {
// Into database.. ??
alert('Workshop saved!');
}).
error(function (data, status, headers, config) {
if (status == 400) {
alert('404');
} else {
alert('Unexpected server error.');
}
});
}
};
});
And my WorkshopController.java looks like this:
> /**
> *
> * @param workshop
> * @param result
> * @return view of the workshop
> */
> @RequestMapping(value = "/workshop/create", method = RequestMethod.POST, headers = "Accept=application/json")
> public String addWorkshop(@Valid Workshop workshop, BindingResult result) throws ParseException {
> if (result.hasErrors()) {
> return BASE_URL + "create";
> } else {
> this.workshopService.addWorkshop(workshop);;
> return "redirect:" + BASE_URL + "view/" + workshop.getId();
> }
> }
I also have a java file named workShopService.java which looks like this
@Service
@Transactional
public class WorkshopService {
@Autowired
private WorkshopDAO workshopDAO;
public void addWorkshop(Workshop workshop) {
workshopDAO.addWorkshop(workshop);
}
public Workshop getWorkshop(int id) {
return workshopDAO.getWorkshop(id);
}
public List<Workshop> getWorkshops() {
return workshopDAO.getWorkshops();
}
}
I started out with SPRING(MVC) and HIBERNATE, now I am confused on what role the Controller has, since with AngularJS you already create a controller.