20

Am I going in a right direction of learning Angular JS?

I'm new to AngularJS but managed to run a jsp file which contain AngularJS code, and made a test calculation/addition and it worked good.

Later when I search web forums, I came to know they are both not supposed to work together as it can cause trouble.

I've used netbeans and GlassFish Server.

Cœur
  • 37,241
  • 25
  • 195
  • 267
user2986018
  • 341
  • 1
  • 2
  • 6
  • Who told you that you cannot use JSP with AngularJS? Sounds like you were given bad advice. – Jacob Mar 11 '16 at 23:59

2 Answers2

15

I wouldn't mix a Servlet/JSP tech with an SPA (single page app....driven by angular in your case). What you can do is use a tool like SpringMVC (or Jersey) that has a single JSP which is your angular driven SPA. That way you have lots of control over the initial HTML/JS/CSS payload in the initial response. Once that "app" is loaded, all it's communication with the server is done via XHR calls ($http or $resource in angular). Spring makes this pretty simple to create using @Controller and giving you all the flexibility you need... things like spring security and dependency injection.

I've been working in my spare time on something simple just like this to help java server side people get into Angular.

the idea is that you'd have a controller that returns a JSP when you make a GET request to

http://your.site.com/contextRoot

That page would have the JS/CSS links to load Bootstrap/JQuery/Angular/Whatever... From there the angular router would kick in and your URL might end up like this

http://...../contextRoot#home

All communication between the JSP running angular on the client and the server is done with $http calls and you can make controllers in Spring to handle all this.

I'll post back here once I have my "Springular" app available

Jason
  • 2,451
  • 2
  • 23
  • 31
  • hi @jason any key takeaways from this exercise? – Paul John Jun 03 '15 at 06:05
  • well, I haven't been able to put as much time towards this as I would have liked, but the limited time I was able to put towards it, I feel like it made AuthZ much simpler. – Jason Dec 22 '15 at 16:09
12

This depends a lot on how you want to use each part. Angular is a full on MVC, so if you're expecting to use a full JSP framework on the backend and Angular on the front end you may end up in some strange situations. It seems like what most people do is serve mostly static Angular on the front end and have it talk to a REST server that they program in whatever back-end language they prefer. So you're really doing two things which end up quite separate: writing the front end MVC code and then writing a back-end REST API.

For some more in-depth discussion, you can check out:

AngularJS client MVC pattern?

or

http://draptik.github.io/blog/2013/07/13/angularjs-example-using-a-java-restful-web-service/

or

AngularJS with Spring-mvc

Community
  • 1
  • 1
urban_raccoons
  • 3,499
  • 1
  • 22
  • 33