1

I am new to Angular JS and I am wondering how routing in Angular JS couples with Spring web MVC. My spring controllers also work on requests and hence, if I am routing to the login page from my home page, like when -

$routeProvider.when(
       '/', {
           templateUrl : 'home/home.html',
           controller : 'HomeCtrl'
       })
       .when(
       '/login', {
           templateUrl : '/login/login.html',
           controller : 'LoginCtrl'
       })
       .when(
       '/register', {
            templateUrl : '/login/signup.html',
           controller : 'RegisterCtrl'
       });

Then even Spring will look for a controller matching the request URL, and will render a view according. But the point is, an entire view should be rendered right? So the entire page will be reloaded, won't it? This takes away the entire point of using routing in Angular. Am i thinking the wrong way? please correct me if what I am thinking is wrong.

1
  • 1
    The spring MVC response needs to be JSON rather than view. Your templates will render this json. Commented May 29, 2015 at 14:58

1 Answer 1

3

There are two main option in your hands:

  1. Build a single page AngularJS application

In this scenario you build a fully fledged AngularJS application where the routing from one page to another one is handled completely by AngularJS with the $routeProvider you are configuring.

Views would not be defined server side. You would generally serve your HTML files as static content. Assuming you put your HTML and JS under /app you would just write in XML configuration:

<mvc:resources location="/app/**" mapping="/app/**" />

or in JavaConfig

@Configuration
public class MyConfig extends WebMvcConfigurerAdapter {


@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/app/**").addResourceLocations("/app/");
}

Once you load your home.html page, the AngularJS application would be bootstrapped and from that moment onwards you would hit the server just to load assets or to perform requests against a RESTful API.

That said you would configure Spring MVC in a way that serves HTML, JS, etc as static resources. That's all.

  1. Build a multi-page application

It is perfectly possible to jump from a page to another and have it that rendered through Spring MVC and the templating engine of your choice server side. However my feeling is that you are experimenting with AngularJS right now, so I would suggest to use the first approach now. This second approach is generally used for very large application or to satisfy very specific scenarios.

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

1 Comment

Thanks, I realized that I need to use Spring as a restful API, that just server me the content.

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.