Share
Embed
Migrating a Spring MVC application to AngularJS
Presented used at the Tokyo Java Spring User Group
Michael Isvy
June 22, 2015
Transcript
46 slides
-
1 1 © Copyright 2013 Pivotal. All rights reserved. Spring
MVC, Angular JS and JHipster Michael Isvy -
2 Michael Isvy Training Manager (Asia-Pacific region) – Joined
SpringSource in 2008 (now part of Pivotal) – Taught Spring in over 20 countries ▪ Core-Spring, Spring MVC, Spring with JPA/Hibernate – Based in Singapore Blog: https://spring.io/team/misvy/ twitter: @michaelisvy -
4 Should you use JSP? (-) Nearly did not
evolve for the past 10 years (-) No Javascript support (+) easy to use -
6 Should you use jQuery? Great AJAX support –
Partial submit, auto-complete, components… Was the de-facto choice 2 years ago -
8 Questions Is there a replacement for JSPs?
Why is jQuery’s popularity now decreasing? -
9 Side by side comparison JSP + jQuery
AngularJS Page templa4ng Yes, using Tiles or JSP custom tags Yes (na;ve to Angular) Form pos4ng JSon request Yes, using jQuery Yes (na;ve to Angular) Client-‐side form valida4on Yes, using jQuery Yes (na;ve to Angular) Par4al refresh Yes, using jQuery Yes (na;ve to Angular) Bootstrap integra4on Yes, manual integra;on Yes (plugin Angular-‐UI) -
10 Outline What is AngularJS? Migrating a JSP
application to AngularJS Choosing between AngularJS and JSP/jQuery Develop faster with JHipster -
11 AngularJS Created by Google in 2009 Controller
layer written in JavaScript View files are HTML files – HTML tags have special attributes called ‘Directives’ Browser compatibility: – Angular 1.4.x supports IE9 and above -
12 Sample controller angular.module('controllers').controller('vetController', ['$scope', '$resource', function($scope, $resource) { var
vetResource = $resource('vets'); $scope.vetList = vetResource.query(); }]); Points to resource at http://localhost:8080/petclinic/vets GET query Result in JSon format VetController.js -
13 Sample view <div data-ng-controller="vetController"> <table> … <tr ng-repeat="vet in
vetList"> <td>{{vet.firstName}} {{vet.lastName}}</td> </tr> </table> </div> Binding to controller ‘Repeat’ loop “Moustache” syntax vetList.html -
14 Single-page application (1/2) What is a single-page application?
index.html index.html Owners list Owner detail Menu bar Menu bar -
15 Single-page application (2/2) Do I have the same
URL for the whole application? – No, URLs can be updated – Bookmarks friendly -
16 Outline What is AngularJS? Migrating a JSP
application to AngularJS Choosing between AngularJS and JSP/jQuery Develop faster with JHipster -
17 Spring Petclinic Sample application being used: Spring Petclinic
– https://github.com/spring-projects/spring-petclinic -
18 Before/After architecture JSP AngularJS @Controller JSP @Repository @Service @RestController
Angular Controller HTML view @Repository @Service Browser Server side JavaScript -
19 Controller in Spring MVC @Controller – Usually not
for REST services – Often used with JSP @RestController – For REST services -
20 @Controller (for JSP, no REST) @Controller public class VetController
{ @RequestMapping("/vets.html") public String showVetList(Map<String, Object> model) { Vets vets = this.clinicService.findVets(); model.put("vets", vets); return "vets/vetList"; } } Name: xController return view name Using model in request scope to communicate with JSP VetController.java -
21 @RestController (for AngularJS) @RestController public class VetResource { @RequestMapping(value="/vets",
method = RequestMethod.GET) public Collection<Vet> showResourcesVetList() { return this.clinicService.findVets(); } } Name: xResource (because Controller is on JavaScript side) VetResource.java -
22 @Controller vs @RestController @Controller @RestController
Class name ends with 'Controller' VetController ends with 'Resource' VetResource Returns model and view Model Object (Vet, Owner…) (view name informa;on on Java Script side) Communica4on with View Layer Uses request/session scope JSon data parsed on JavaScript side -
23 IDE support Support in Eclipse is limited –
Angular-Eclipse plugin being developed – Not stable enough at this stage Just work with HTML files and use ‘data’ prefix -
24 Step by step migration Migrate Spring MVC controllers
to REST resources – Also using Spring MVC Create AngularJS controllers Migrate JSP files to HTML files – Using AngularJS directives -
25 REST resources Setting up REST in Spring MVC
is easy – Include JSon lib in the classpath – Controllers should be annotated with @RestController -
26 Preparing Data Transfer Objects Make sure minimum data
will be serialised – Create DTOs if needed – @JSonIgnore Bidirectionnal relationships: choose one side Owner Pet @JSonIgnore -
27 Spring MVC Controllers/Resources Rename Controllers into Resources
Make sure that all methods return objects – Not ModelAndView -
28 AngularJS side Prepare all dependencies on Bower
Bower is similar to Maven but for web/static dependencies – JavaScript (jQuery, AngularJS…) – HTML (bootstrap) -
29 Bower samples { "name": "petclinic-angular", "version": "0.0.0", "appPath": "src/main/webapp",
"dependencies": { "bootstrap": "2.3.0", "jquery": "2.1.3", "json3": "3.3.2", "angular": "1.3.11", "angular-route": "1.3.11", "angular-resource": "1.3.11" } } ‘bower update’ //updates all dependencies bower.json -
30 Generic files: index.html Contains the template for the
single page application index.html Owners list Menu bar -
31 index.html <html ng-app="petClinicApp"> <head> <title>PetClinic</title <script src="bower_components/angular/angular.js"></script> <!-- import
for all scripts and CSS --> </head> <body class="container"> <div ng-include="'app/fragments/bodyHeader.html'"></div> <div ng-view></div> </body> </html> index.html Main PetClinic module Value inside app.js -
32 Generic files: app.js Contains navigation rules when('/vets', {
templateUrl: 'scripts/app/vet/vetList.html', controller: 'vetController’ }). otherwise({ redirectTo: '/’ }); }]); app.js -
33 app.js when('/vets', { templateUrl: 'scripts/app/vet/vetList.html', controller: 'vetController’ }). otherwise({
redirectTo: '/’ }); }]); Request on: http://localhost:8080/petclinic/#/vets <body> <div ng-view></div> </body> index.html app.js -
35 Outline What is AngularJS? Migrating a JSP
application to AngularJS Choosing between AngularJS and JSP/jQuery Develop faster with JHipster -
36 AngularJS will be easy for you if… You
are experienced with JavaScript You are experienced with jQuery You are experienced with REST in Spring MVC – Or with JAX-RS as an alternative to Spring MVC -
37 Angular JS will require more time if… You
have little experience with JavaScript and jQuery You have never used REST on Java side -
39 Pros for AngularJS (+) Hot redeploy – no
need to restart Tomcat – Unless you have changed Java side (+) HTML pages are very clean (+) AJAX is built in (+) Many plugins available (+) Web Designer can run JavaScript side with Mock Data -
40 Cons for AngularJS (-) many ways to do
the same thing – 8 ways to write a controller! (-) Error messages sometimes not clear (-) Limited IDE support -
41 Should you use AngularJS? – Do you need partial
refresh? – Do you need auto-complete? – Do you need validation on the client side? – Are you thinking to create a mobile application for your site? If yes, AngularJS is likely to be simpler than using JSP If not, just use JSP and use jQuery when needed -
42 Outline What is AngularJS? Migrating a JSP
application to AngularJS Choosing between AngularJS and JSP/jQuery Develop faster with JHipster -
43 What is JHipster? Spring MVC and AngularJS code
generator – With all the best practices Created in 2014 and fast growing! http://jhipster.github.io/ -
45 What can JHipster do for you? Security
Internationalization Form validation Development/Production optimizations … -
46 Security When working with REST, all URLs need
to be secured – Best framework for that is Spring Security Tedious: all URLs need to be secured