0

I plan to use Spring MVC + angularJS for an j2ee application. The questions are as follows :-

  1. Is it possible to use AngularJS with Spring MVC, i am not talking about the Rest Services with Spring configuration here. Its Spring MVC framework to build the webapplication.If so kindly please throw light with an example.
  2. I did google and found out like people use spring mvc and restful services synonymously,the google results which i got was like angularJS + spring mvc rest services,where in the request response is in the JSON format, i am not sure if thats the reason they say its Rest service.

Thank you

user1125814
  • 87
  • 1
  • 4
  • Check out Spring Data REST, as well as Spring HATEOAS for building Restful web services. Alternatively, check out Jersey. – Neil McGuigan Sep 23 '13 at 01:54

2 Answers2

4

First of all, AngularJS is a Front-End MVC Framework and it doesn't matter what technology you use on the Back-End. May it be Java (Spring, Struts or any other framework) or .NET.

And to get your response as JSON, you got to include 'Jackson JSON Processor`. Here is one the question related to that.

Spring: Return JSON response From A Java Bean

Also to handle you AngularJS code, here the question.

Spring MVC and Angularjs

Cheers! Have fun with Angular!

Community
  • 1
  • 1
Abilash
  • 6,089
  • 6
  • 25
  • 30
  • You should accept the answer if you have found what you are looking for. If not please convey what's missing. In this way others can look at this question with conviction – Abilash Nov 24 '13 at 16:59
0

I am making a similar implementation starting from this example https://github.com/xvitcoder/spring-mvc-angularjs. The main difference with the sample

a) put all the AngularJS HTML files under webapp/WEB-INF/views

b) have a Spring ViewResolver mapped on /views. We are using Velocity but you can you use freechart or whatever view technology.

<bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<property name="resourceLoaderPath" value="/WEB-INF/views/">
</bean><!-- see xvitcoder sample for the complete web.xml-->

c) have a Controller that respond to your HTML partial requests. The magic is that the client side URL doesn't need to match the physical folder structure. This is something lacking when you have a static html project.

@RequestMapping("dummypath/{folder}/{remainder}")
public String getSomePartial(@PathVariable("folder") String folder,
@PathVariable("remainder") String path) {
return folder + "/" + path;
}

dummpypath allows to isolate the requests for AngularJS partials (ngInclude, ngView) from other requests like servlet or REST service. The HTML files are still actually under /views/folder/path. You can do more complex stuff like computing a different view name.

<div ng-include="dummypath/summary/table.html"></div>

will load the views/summary/table.html file. Your ViewResolver will preprocess this file beforehand, allowing some server side fixes.

d) Example of passing parameters to angularjs from Spring MVC. Define the query in your Spring Controller and return a html file

@RequestMapping
public String getPage(@RequestParam(value="someparam", defaultValue="0") string myparam,
ModelMap model){
model.addAttribute("someparam", myparam)
return "index"; //index.html
}

In the AngularJs controller inject $location to retrieve the parameter.

$scope.someparam = ($location.search()).someparam

Once the index is loaded, we are in Angular ecosystem! I am using this method to refresh the webpage while retaining the context. Based on the query parameter, you can load some JSON stuff or have ngSwitch put on the right partial.

e) PROS and CONS

PROS: simply you can't always build a complete solution with static files + REST

CONS: testing and building the project requires extra steps. You need to bundle stuff in a WAR, testing them outside and inside.

mahery rafara
  • 368
  • 4
  • 13