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.

3 Answers 3

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.

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

5 Comments

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.
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 github.com/witoldsz/angular-http-auth
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
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
@PriyankThakkar there is a good tutorial from spring team about angularjs + spring security spring.io/guides/tutorials/spring-security-and-angular-js
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:

1 Comment

Thank you mite. I will look into these projects and discussions.
0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.