Slide 1

Slide 1 text

1 1 © Copyright 2013 Pivotal. All rights reserved. Spring MVC, Angular JS and JHipster Michael Isvy

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

3 Please complete this survey http://tinyurl.com/pivotaltokyo

Slide 4

Slide 4 text

4 Should you use JSP? Ÿ  (-) Nearly did not evolve for the past 10 years Ÿ  (-) No Javascript support Ÿ  (+) easy to use

Slide 5

Slide 5 text

5 JSP: interest over time (Google trend)

Slide 6

Slide 6 text

6 Should you use jQuery? Ÿ  Great AJAX support –  Partial submit, auto-complete, components… Ÿ  Was the de-facto choice 2 years ago

Slide 7

Slide 7 text

7 jQuery: interest over time (Google trend) Maximum in 2013

Slide 8

Slide 8 text

8 Questions Ÿ  Is there a replacement for JSPs? Ÿ  Why is jQuery’s popularity now decreasing?

Slide 9

Slide 9 text

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)  

Slide 10

Slide 10 text

10 Outline Ÿ  What is AngularJS? Ÿ  Migrating a JSP application to AngularJS Ÿ  Choosing between AngularJS and JSP/jQuery Ÿ  Develop faster with JHipster

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

13 Sample view
… {{vet.firstName}} {{vet.lastName}}
Binding to controller ‘Repeat’ loop “Moustache” syntax vetList.html

Slide 14

Slide 14 text

14 Single-page application (1/2) Ÿ  What is a single-page application? index.html index.html Owners list Owner detail Menu bar Menu bar

Slide 15

Slide 15 text

15 Single-page application (2/2) Ÿ  Do I have the same URL for the whole application? –  No, URLs can be updated –  Bookmarks friendly

Slide 16

Slide 16 text

16 Outline Ÿ  What is AngularJS? Ÿ  Migrating a JSP application to AngularJS Ÿ  Choosing between AngularJS and JSP/jQuery Ÿ  Develop faster with JHipster

Slide 17

Slide 17 text

17 Spring Petclinic Ÿ  Sample application being used: Spring Petclinic –  https://github.com/spring-projects/spring-petclinic

Slide 18

Slide 18 text

18 Before/After architecture JSP AngularJS @Controller JSP @Repository @Service @RestController Angular Controller HTML view @Repository @Service Browser Server side JavaScript

Slide 19

Slide 19 text

19 Controller in Spring MVC Ÿ  @Controller –  Usually not for REST services –  Often used with JSP Ÿ  @RestController –  For REST services

Slide 20

Slide 20 text

20 @Controller (for JSP, no REST) @Controller public class VetController { @RequestMapping("/vets.html") public String showVetList(Map 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

Slide 21

Slide 21 text

21 @RestController (for AngularJS) @RestController public class VetResource { @RequestMapping(value="/vets", method = RequestMethod.GET) public Collection showResourcesVetList() { return this.clinicService.findVets(); } } Name: xResource (because Controller is on JavaScript side) VetResource.java

Slide 22

Slide 22 text

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    

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

25 REST resources Ÿ  Setting up REST in Spring MVC is easy –  Include JSon lib in the classpath –  Controllers should be annotated with @RestController

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

27 Spring MVC Controllers/Resources Ÿ  Rename Controllers into Resources Ÿ  Make sure that all methods return objects –  Not ModelAndView

Slide 28

Slide 28 text

28 AngularJS side Ÿ  Prepare all dependencies on Bower Ÿ  Bower is similar to Maven but for web/static dependencies –  JavaScript (jQuery, AngularJS…) –  HTML (bootstrap)

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

30 Generic files: index.html Ÿ  Contains the template for the single page application index.html Owners list Menu bar

Slide 31

Slide 31 text

31 index.html PetClinic
index.html Main PetClinic module Value inside app.js

Slide 32

Slide 32 text

32 Generic files: app.js Ÿ  Contains navigation rules when('/vets', { templateUrl: 'scripts/app/vet/vetList.html', controller: 'vetController’ }). otherwise({ redirectTo: '/’ }); }]); app.js

Slide 33

Slide 33 text

33 app.js when('/vets', { templateUrl: 'scripts/app/vet/vetList.html', controller: 'vetController’ }). otherwise({ redirectTo: '/’ }); }]); Request on: http://localhost:8080/petclinic/#/vets
index.html app.js

Slide 34

Slide 34 text

34 Demo Ÿ  Show breakpoint

Slide 35

Slide 35 text

35 Outline Ÿ  What is AngularJS? Ÿ  Migrating a JSP application to AngularJS Ÿ  Choosing between AngularJS and JSP/jQuery Ÿ  Develop faster with JHipster

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

37 Angular JS will require more time if… Ÿ  You have little experience with JavaScript and jQuery Ÿ  You have never used REST on Java side

Slide 38

Slide 38 text

38 AngularJS Pros and Cons

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

42 Outline Ÿ  What is AngularJS? Ÿ  Migrating a JSP application to AngularJS Ÿ  Choosing between AngularJS and JSP/jQuery Ÿ  Develop faster with JHipster

Slide 43

Slide 43 text

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/

Slide 44

Slide 44 text

44 JHipster Demo

Slide 45

Slide 45 text

45 What can JHipster do for you? Ÿ  Security Ÿ  Internationalization Ÿ  Form validation Ÿ  Development/Production optimizations Ÿ  …

Slide 46

Slide 46 text

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