0

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.

1
  • The angular controllers and the spring controllers are different. The angular controller has the responsibility for routing inside your frontend. The Spring controller has the responsibility to call the persistence layer when you need it You could use SpringREST intead of Spring MVC and create a RESTFUL API . Commented Dec 10, 2015 at 13:11

1 Answer 1

1

In Spring you have the business logic. So it's in charge of connecting the model and the view, and is used to communicate between classes in the model and view. This model is the workshop class and the view is the json response.

In the angular application you have also a MVC pattern. The controller has the same function (connect the model and the view), but in this case the view is the html and the model is the workshop javascript object.

Sign up to request clarification or add additional context in comments.

3 Comments

Hello reos, thanks for the information. Does this mean that I need to replace the controllers for AngularJS controllers? and could you give me an example? Thanks in advance
I think this is a good example: link
I think you're doing it in the rigth way. There are many things that you can't manage int he client side (AngularJs), e.g Database connection.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.