43

I recently came across AngularJS. I am a java web developer. I want to use AngularJS along with Spring-MVC framework. But I need some basic foundation (tutorial) to start with.

I know about the basic stuff of AngularJS, but how to integrate it with Spring-MVC. As most of the time spring it self return partial jsp(s), and we add them using jsp-include, while AngularJS expects JSON data most of the time.

Priyank Thakkar
  • 4,752
  • 19
  • 57
  • 93

3 Answers3

53

Two cases:

  1. Your architecture is full client-side: In this case the integration is very natural. Spring MVC exposes your service as a REST (JSON/XML) and your client application with AngularJS consumes your JSON. Here the .war application (Spring MVC) must be deployed in a servlet container (e.g. Tomcat) and your client application can be deployed in the same server or in a HTTP server like Nginx or Apache.

  2. You want to keep page generation in the server-side and only use AngularJS for some DOM manipulation so your code must be deployed in the same .war (inside the WEB-INF/ folder).

Mixing the two approaches in not always a good idea. You can try Thymeleaf to stay in server-side page generation and at the same time have templating, testability and clean View code.

kryger
  • 12,906
  • 8
  • 44
  • 65
Nabil
  • 1,771
  • 4
  • 21
  • 33
  • Thank you for the insight. As far as war (Spring-MVC) is concerned I have the application ready with REST service. It was just should directly have .html pages in my views or I should use as usual .jsp page with AngularJS because finally when they are rendered on client side they are html. – Priyank Thakkar Sep 02 '13 at 12:38
  • 3
    If your application is full REST , you don't need to go with JSP . It will introduce complexity , use only HTML files . Pay attention about you Model of Security and authorization , Backend generation(JSP) and the use of spring security make things very easy with the JSP tag lib of spring security , you will need to handle this manually in the client side . This nice directive can help you https://github.com/witoldsz/angular-http-auth/ – Nabil Sep 10 '13 at 10:01
  • Thanks Nabil. I will try that. At present I have kept my shell as jsp page. All the partials are .html, but I will definitely look into the github link you gave. With HTML I was worried about security – Priyank Thakkar Sep 10 '13 at 10:30
  • You are right to be worried about security , not about the reliability but about the effort of implementing functionality that spring-security do for you in one line of code . Good Luck – Nabil Sep 10 '13 at 10:56
  • @PriyankThakkar there is a good tutorial from spring team about angularjs + spring security https://spring.io/guides/tutorials/spring-security-and-angular-js/ – Boris Mitioglov Feb 15 '16 at 11:02
36

Just set angular.js files as static files . There should be nothing private there anyway.

Use Spring MVC as a REST Service provider instead of JSP/Velocity/Any other Template view resolver.

You have sample projects on the folowing sites:

Community
  • 1
  • 1
Mite Mitreski
  • 3,596
  • 3
  • 29
  • 39
0

If you like to learn by example you can try to generate some code with jhipster: https://jhipster.github.io/

fazerty
  • 410
  • 3
  • 5