groovy & grails - dr/xpose2009/groovy_and_grails/download/... · plan introduction groovy...

57
Xposé 2010 Martin Le Bas Groovy & Grails Langage de script basé sur Java appliqué dans un environnement JEE 1

Upload: phamkiet

Post on 11-Oct-2018

229 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Groovy & Grails - dr/XPOSE2009/Groovy_and_Grails/download/... · Plan Introduction Groovy Grails Conclusion Références Plan de la présentation • Introduction • Groovy ‣ Du

Xposé 2010Martin Le Bas

Groovy & GrailsLangage de script basé sur Java

appliqué dans un environnement JEE

1

Page 2: Groovy & Grails - dr/XPOSE2009/Groovy_and_Grails/download/... · Plan Introduction Groovy Grails Conclusion Références Plan de la présentation • Introduction • Groovy ‣ Du

Plan Introduction Groovy Grails Conclusion Références

Plan de la présentation

• Introduction

• Groovy

‣ Du Java au Groovy

‣ Le langage Groovy

‣ Groovy avancé

• Grails

‣ Architecture

‣ Créer une application web

• Conclusion

• Références2

Plan

Page 3: Groovy & Grails - dr/XPOSE2009/Groovy_and_Grails/download/... · Plan Introduction Groovy Grails Conclusion Références Plan de la présentation • Introduction • Groovy ‣ Du

Plan Introduction Groovy Grails Conclusion Références

3

Pourquoi ce nouveau langage ?

• Caractéristiques d’un langage

Typage : Fort ? Faible ?

Langage Objet ?

Langage de Script ?

Flexibilité ? Maintenance ?

Productivité ?

• Langages statiques : Java, C#, C++

• Langages dynamiques : Perl, Python, Ruby, PHP

• Plateformes .Net et Java : IronRuby, IronPython, JRuby ...

Introduction

Page 4: Groovy & Grails - dr/XPOSE2009/Groovy_and_Grails/download/... · Plan Introduction Groovy Grails Conclusion Références Plan de la présentation • Introduction • Groovy ‣ Du

Du java au Groovy Le langage Groovy Groovy avancé

Groovy : késako ?

• C’est quoi ?

Langage de script utilisant la syntaxe de Java

Compatibilité du code Java

Faiblement typé

Vérification des types à l'exécution

• Comment ?

Utilise la JVM : Profite du JIT Compiler

Compilation et Exécution

‣ javac & java

‣ groovy

4

Du java au Groovy

Page 5: Groovy & Grails - dr/XPOSE2009/Groovy_and_Grails/download/... · Plan Introduction Groovy Grails Conclusion Références Plan de la présentation • Introduction • Groovy ‣ Du

5

Page 6: Groovy & Grails - dr/XPOSE2009/Groovy_and_Grails/download/... · Plan Introduction Groovy Grails Conclusion Références Plan de la présentation • Introduction • Groovy ‣ Du

Du java au Groovy Le langage Groovy Groovy avancé

• Getters et Setters implémentés par défaut.

• Utilisation simplifiée des setters :

• Typage dynamique : Vérification à l'exécution

• Retourne l’objet de la dernière ligne d’une méthode

• Simplification de la concaténation

• Suppression des points virgules

• Suppression du main : Script

6

Simplification du codeDu java au Groovy

Page 7: Groovy & Grails - dr/XPOSE2009/Groovy_and_Grails/download/... · Plan Introduction Groovy Grails Conclusion Références Plan de la présentation • Introduction • Groovy ‣ Du

7

Page 8: Groovy & Grails - dr/XPOSE2009/Groovy_and_Grails/download/... · Plan Introduction Groovy Grails Conclusion Références Plan de la présentation • Introduction • Groovy ‣ Du

Du java au Groovy Le langage Groovy Groovy avancé

8

Le langage Groovy

• Chaines de caractères

• Constructeur

• Méthodes

• Closures

• Collections

• Opérateurs

• Expressions régulières

Le langage Groovy

Page 9: Groovy & Grails - dr/XPOSE2009/Groovy_and_Grails/download/... · Plan Introduction Groovy Grails Conclusion Références Plan de la présentation • Introduction • Groovy ‣ Du

Du java au Groovy Le langage Groovy Groovy avancé

9

Chaines de caractères

• Slashy string

• Multiline string

• GString

Le langage Groovy

Page 10: Groovy & Grails - dr/XPOSE2009/Groovy_and_Grails/download/... · Plan Introduction Groovy Grails Conclusion Références Plan de la présentation • Introduction • Groovy ‣ Du

Du java au Groovy Le langage Groovy Groovy avancé

10

Constructeur

• Utilisation d’une Map pour initialiser un GroovyBean.

Le langage Groovy

Page 11: Groovy & Grails - dr/XPOSE2009/Groovy_and_Grails/download/... · Plan Introduction Groovy Grails Conclusion Références Plan de la présentation • Introduction • Groovy ‣ Du

Du java au Groovy Le langage Groovy Groovy avancé

• public par défaut

• pas de type

• pas de « return »

• Peut être utilisé comme une « fonction »

11

MéthodesLe langage Groovy

Page 12: Groovy & Grails - dr/XPOSE2009/Groovy_and_Grails/download/... · Plan Introduction Groovy Grails Conclusion Références Plan de la présentation • Introduction • Groovy ‣ Du

Du java au Groovy Le langage Groovy Groovy avancé

• Sans argument

• Avec arguments

• Crée une classe interne avec une méthode « call » qui exécute les instructions.

• Comme pour les méthodes, les parenthèses sont facultatives si il existe des arguments.

• Peut être passé en argument d’une méthode ou d’une autre closure.

12

ClosureLe langage Groovy

Page 13: Groovy & Grails - dr/XPOSE2009/Groovy_and_Grails/download/... · Plan Introduction Groovy Grails Conclusion Références Plan de la présentation • Introduction • Groovy ‣ Du

Du java au Groovy Le langage Groovy Groovy avancé

• Passage d’une closure à une méthode.

13

Méthode & ClosureLe langage Groovy

Page 14: Groovy & Grails - dr/XPOSE2009/Groovy_and_Grails/download/... · Plan Introduction Groovy Grails Conclusion Références Plan de la présentation • Introduction • Groovy ‣ Du

Du java au Groovy Le langage Groovy Groovy avancé

• List

• Set

14

CollectionsLe langage Groovy

Page 15: Groovy & Grails - dr/XPOSE2009/Groovy_and_Grails/download/... · Plan Introduction Groovy Grails Conclusion Références Plan de la présentation • Introduction • Groovy ‣ Du

Du java au Groovy Le langage Groovy Groovy avancé

• Map

• Range

15

CollectionsLe langage Groovy

Page 16: Groovy & Grails - dr/XPOSE2009/Groovy_and_Grails/download/... · Plan Introduction Groovy Grails Conclusion Références Plan de la présentation • Introduction • Groovy ‣ Du

Du java au Groovy Le langage Groovy Groovy avancé

• Possibilité de surcharger l’opérateur :

16

OpérateursLe langage Groovy

a + b a.plus(b)

a * b a.multiply(b)

a << b a.leftShift(b)

a & b a.and(b)

a++ a.next()

a[b] a.getAt(b)

a[b]=c a.putAt(b,c)

Page 17: Groovy & Grails - dr/XPOSE2009/Groovy_and_Grails/download/... · Plan Introduction Groovy Grails Conclusion Références Plan de la présentation • Introduction • Groovy ‣ Du

Du java au Groovy Le langage Groovy Groovy avancé

• Nouveaux opérateurs :

Spread operator « *. »

17

OpérateursLe langage Groovy

Page 18: Groovy & Grails - dr/XPOSE2009/Groovy_and_Grails/download/... · Plan Introduction Groovy Grails Conclusion Références Plan de la présentation • Introduction • Groovy ‣ Du

Du java au Groovy Le langage Groovy Groovy avancé

• Nouveaux opérateurs :

Elvis operator « ?: »

Safe navigation operator « ?. »

Field operator « .@ »

18

OpérateursLe langage Groovy

Page 19: Groovy & Grails - dr/XPOSE2009/Groovy_and_Grails/download/... · Plan Introduction Groovy Grails Conclusion Références Plan de la présentation • Introduction • Groovy ‣ Du

Du java au Groovy Le langage Groovy Groovy avancé

Expressions régulières

• Match operator : ==~

• Find operator : =~

19

Le langage Groovy

Page 20: Groovy & Grails - dr/XPOSE2009/Groovy_and_Grails/download/... · Plan Introduction Groovy Grails Conclusion Références Plan de la présentation • Introduction • Groovy ‣ Du

Du java au Groovy Le langage Groovy Groovy avancé

Expressions régulières

• Pattern operator : ~string

20

Le langage Groovy

Page 21: Groovy & Grails - dr/XPOSE2009/Groovy_and_Grails/download/... · Plan Introduction Groovy Grails Conclusion Références Plan de la présentation • Introduction • Groovy ‣ Du

Du java au Groovy Le langage Groovy Groovy avancé

Groovy avancé

• Meta Object Protocol (MOP)

• Gestion d’XML

• Templates

• Tests unitaires

21

Groovy avancé

Page 22: Groovy & Grails - dr/XPOSE2009/Groovy_and_Grails/download/... · Plan Introduction Groovy Grails Conclusion Références Plan de la présentation • Introduction • Groovy ‣ Du

Du java au Groovy Le langage Groovy Groovy avancé

Meta Object Protocol

• Exemple simple

22

Groovy avancé

Page 23: Groovy & Grails - dr/XPOSE2009/Groovy_and_Grails/download/... · Plan Introduction Groovy Grails Conclusion Références Plan de la présentation • Introduction • Groovy ‣ Du

Du java au Groovy Le langage Groovy Groovy avancé

Meta Object Protocol

• Autre exemple : Foreach

23

Groovy avancé

Page 24: Groovy & Grails - dr/XPOSE2009/Groovy_and_Grails/download/... · Plan Introduction Groovy Grails Conclusion Références Plan de la présentation • Introduction • Groovy ‣ Du

Du java au Groovy Le langage Groovy Groovy avancé

XML

• Génération d’XML avec un MarkupBuilder

24

Groovy avancé

Page 25: Groovy & Grails - dr/XPOSE2009/Groovy_and_Grails/download/... · Plan Introduction Groovy Grails Conclusion Références Plan de la présentation • Introduction • Groovy ‣ Du

Du java au Groovy Le langage Groovy Groovy avancé

XML• Parsing d’XML avec un XmlSlurper

25

Groovy avancé

Page 26: Groovy & Grails - dr/XPOSE2009/Groovy_and_Grails/download/... · Plan Introduction Groovy Grails Conclusion Références Plan de la présentation • Introduction • Groovy ‣ Du

Du java au Groovy Le langage Groovy Groovy avancé

Templates• Utilisation du SimpleTemplateEngine

26

Groovy avancé

Page 27: Groovy & Grails - dr/XPOSE2009/Groovy_and_Grails/download/... · Plan Introduction Groovy Grails Conclusion Références Plan de la présentation • Introduction • Groovy ‣ Du

Du java au Groovy Le langage Groovy Groovy avancé

Templates• Utilisation du SimpleTemplateEngine

26

Groovy avancé

Page 28: Groovy & Grails - dr/XPOSE2009/Groovy_and_Grails/download/... · Plan Introduction Groovy Grails Conclusion Références Plan de la présentation • Introduction • Groovy ‣ Du

Du java au Groovy Le langage Groovy Groovy avancé

Tests Unitaires

• Groovy Unit Testing

Primordial

Facilite la maintenance

Groovy étend JUnit pour l’adapter aux particularités du langage.

‣ assertArrayEquals

‣ assertContains

‣ assertEquals

‣ assertInspect

‣ assertScript

‣ etc...

27

Groovy avancé

Page 29: Groovy & Grails - dr/XPOSE2009/Groovy_and_Grails/download/... · Plan Introduction Groovy Grails Conclusion Références Plan de la présentation • Introduction • Groovy ‣ Du

28

Page 30: Groovy & Grails - dr/XPOSE2009/Groovy_and_Grails/download/... · Plan Introduction Groovy Grails Conclusion Références Plan de la présentation • Introduction • Groovy ‣ Du

28

Page 31: Groovy & Grails - dr/XPOSE2009/Groovy_and_Grails/download/... · Plan Introduction Groovy Grails Conclusion Références Plan de la présentation • Introduction • Groovy ‣ Du

Architecture Créer une application web

29

GRAILS

• Architecture

• Créer une application web

scaffolding

controller

domain

validation

services

views

taglib

plug-in

Page 32: Groovy & Grails - dr/XPOSE2009/Groovy_and_Grails/download/... · Plan Introduction Groovy Grails Conclusion Références Plan de la présentation • Introduction • Groovy ‣ Du

Architecture Créer une application web

30

ArchitectureArchitecture

GRAILS Framework

Java Enterprise Edition

Java Virtual Machine

Groovy

Application GRAILS

Page 33: Groovy & Grails - dr/XPOSE2009/Groovy_and_Grails/download/... · Plan Introduction Groovy Grails Conclusion Références Plan de la présentation • Introduction • Groovy ‣ Du

Architecture Créer une application web

30

ArchitectureArchitecture

GRAILS Framework

Java Enterprise Edition

Java Virtual Machine

Groovy

Application GRAILS

SiteMesh(OpenSymphony)

Page 34: Groovy & Grails - dr/XPOSE2009/Groovy_and_Grails/download/... · Plan Introduction Groovy Grails Conclusion Références Plan de la présentation • Introduction • Groovy ‣ Du

Architecture Créer une application web

30

ArchitectureArchitecture

GRAILS Framework

Java Enterprise Edition

Java Virtual Machine

Groovy

Application GRAILS

SPRINGSiteMesh(OpenSymphony)

Page 35: Groovy & Grails - dr/XPOSE2009/Groovy_and_Grails/download/... · Plan Introduction Groovy Grails Conclusion Références Plan de la présentation • Introduction • Groovy ‣ Du

Architecture Créer une application web

30

ArchitectureArchitecture

GRAILS Framework

Java Enterprise Edition

Java Virtual Machine

Groovy

Application GRAILS

SPRINGSiteMesh(OpenSymphony) Hibernate

HSQLDB

GORM

Page 36: Groovy & Grails - dr/XPOSE2009/Groovy_and_Grails/download/... · Plan Introduction Groovy Grails Conclusion Références Plan de la présentation • Introduction • Groovy ‣ Du

Architecture Créer une application web

30

ArchitectureArchitecture

GRAILS Framework

Java Enterprise Edition

Java Virtual Machine

Groovy

Application GRAILS

SPRINGSiteMesh(OpenSymphony) Hibernate

HSQLDB

GORM

Controllers

Views (gsp) Services & Plug-in Domains

Page 37: Groovy & Grails - dr/XPOSE2009/Groovy_and_Grails/download/... · Plan Introduction Groovy Grails Conclusion Références Plan de la présentation • Introduction • Groovy ‣ Du

Architecture Créer une application web

30

ArchitectureArchitecture

GRAILS Framework

Java Enterprise Edition

Java Virtual Machine

Groovy

Application GRAILS

SPRINGSiteMesh(OpenSymphony) Hibernate

HSQLDB

GORM

Controllers

Views (gsp) Services & Plug-in Domains

Page 38: Groovy & Grails - dr/XPOSE2009/Groovy_and_Grails/download/... · Plan Introduction Groovy Grails Conclusion Références Plan de la présentation • Introduction • Groovy ‣ Du

Architecture Créer une application web

30

ArchitectureArchitecture

GRAILS Framework

Java Enterprise Edition

Java Virtual Machine

Groovy

Application GRAILS

SPRINGSiteMesh(OpenSymphony) Hibernate

HSQLDB

GORM

Controllers

Views (gsp) Services & Plug-in Domains

Page 39: Groovy & Grails - dr/XPOSE2009/Groovy_and_Grails/download/... · Plan Introduction Groovy Grails Conclusion Références Plan de la présentation • Introduction • Groovy ‣ Du

Architecture Créer une application web

30

ArchitectureArchitecture

GRAILS Framework

Java Enterprise Edition

Java Virtual Machine

Groovy

Application GRAILS

SPRINGSiteMesh(OpenSymphony) Hibernate

HSQLDB

GORM

Controllers

Views (gsp) Services & Plug-in Domains

Page 40: Groovy & Grails - dr/XPOSE2009/Groovy_and_Grails/download/... · Plan Introduction Groovy Grails Conclusion Références Plan de la présentation • Introduction • Groovy ‣ Du

Architecture Créer une application web

30

ArchitectureArchitecture

GRAILS Framework

Java Enterprise Edition

Java Virtual Machine

Groovy

Application GRAILS

SPRINGSiteMesh(OpenSymphony) Hibernate

HSQLDB

GORM

Controllers

Views (gsp) Services & Plug-in Domains

Page 41: Groovy & Grails - dr/XPOSE2009/Groovy_and_Grails/download/... · Plan Introduction Groovy Grails Conclusion Références Plan de la présentation • Introduction • Groovy ‣ Du

Architecture Créer une application web

30

ArchitectureArchitecture

GRAILS Framework

Java Enterprise Edition

Java Virtual Machine

Groovy

Application GRAILS

SPRINGSiteMesh(OpenSymphony) Hibernate

HSQLDB

GORM

Controllers

Views (gsp) Services & Plug-in Domains

Page 42: Groovy & Grails - dr/XPOSE2009/Groovy_and_Grails/download/... · Plan Introduction Groovy Grails Conclusion Références Plan de la présentation • Introduction • Groovy ‣ Du

Architecture Créer une application web

30

ArchitectureArchitecture

GRAILS Framework

Java Enterprise Edition

Java Virtual Machine

Groovy

Application GRAILS

SPRINGSiteMesh(OpenSymphony) Hibernate

HSQLDB

GORM

Controllers

Views (gsp) Services & Plug-in Domains

Gan

t

Page 43: Groovy & Grails - dr/XPOSE2009/Groovy_and_Grails/download/... · Plan Introduction Groovy Grails Conclusion Références Plan de la présentation • Introduction • Groovy ‣ Du

Architecture Créer une application web

Les commandes grails

• grails create-app myApp

• grails create-domain-class my.pckg.DomainClass

• grails generate-controller my.pckg.DomainClass

• grails generate-views my.pckg.DomainClass

• grails create-service my.pckg.ServiceName

• grails clean

• grails run-app

31

Créer une application web

Page 44: Groovy & Grails - dr/XPOSE2009/Groovy_and_Grails/download/... · Plan Introduction Groovy Grails Conclusion Références Plan de la présentation • Introduction • Groovy ‣ Du

Architecture Créer une application web

Scaffolding

• Génération des vues et des «Controllers» en fonction de la définition des «Domains».

Vues & Actions générées :

‣ Création

‣ Edition

‣ Suppression

‣ Affichage

‣ Liste

Le scaffolding peut être dynamique ou statique.

32

Créer une application web

Page 45: Groovy & Grails - dr/XPOSE2009/Groovy_and_Grails/download/... · Plan Introduction Groovy Grails Conclusion Références Plan de la présentation • Introduction • Groovy ‣ Du

33

Page 46: Groovy & Grails - dr/XPOSE2009/Groovy_and_Grails/download/... · Plan Introduction Groovy Grails Conclusion Références Plan de la présentation • Introduction • Groovy ‣ Du

Architecture Créer une application web

Controller

• Reçoit la requête et effectue le traitement en fonction de l’action demandée.

• Mappage des URLs : http://<... >/controller/action/

• Exemple :

34

Créer une application web

Page 47: Groovy & Grails - dr/XPOSE2009/Groovy_and_Grails/download/... · Plan Introduction Groovy Grails Conclusion Références Plan de la présentation • Introduction • Groovy ‣ Du

Architecture Créer une application web

Domain• Modélise les données

• Permet de mapper les objets dans une base de données.

• Définit le comportement de l’application.

• Facilite la validation des formulaires.

• Query caching

35

Créer une application web

• MOP : get(), save(), list(), findByFirstname(), etc...

Page 48: Groovy & Grails - dr/XPOSE2009/Groovy_and_Grails/download/... · Plan Introduction Groovy Grails Conclusion Références Plan de la présentation • Introduction • Groovy ‣ Du

Architecture Créer une application web

Validation

36

Créer une application web

Page 49: Groovy & Grails - dr/XPOSE2009/Groovy_and_Grails/download/... · Plan Introduction Groovy Grails Conclusion Références Plan de la présentation • Introduction • Groovy ‣ Du

Architecture Créer une application web

Validation

36

Créer une application web

Page 50: Groovy & Grails - dr/XPOSE2009/Groovy_and_Grails/download/... · Plan Introduction Groovy Grails Conclusion Références Plan de la présentation • Introduction • Groovy ‣ Du

Architecture Créer une application web

Services

• Scope (durée de vie)

singleton

session

conversation

flow

flash

request

prototype

• Utilisation d’un service par « injection »

37

Créer une application web

Page 51: Groovy & Grails - dr/XPOSE2009/Groovy_and_Grails/download/... · Plan Introduction Groovy Grails Conclusion Références Plan de la présentation • Introduction • Groovy ‣ Du

Architecture Créer une application web

Vues

• GSP : Groovy Server Page

• Utilisation de SiteMesh pour «décorer» les pages.

• Intégration de librairies AJAX dans le framework. (taglib)

38

Créer une application web

Page 52: Groovy & Grails - dr/XPOSE2009/Groovy_and_Grails/download/... · Plan Introduction Groovy Grails Conclusion Références Plan de la présentation • Introduction • Groovy ‣ Du

Architecture Créer une application web

TagLib Grails

39

Créer une application web

Page 53: Groovy & Grails - dr/XPOSE2009/Groovy_and_Grails/download/... · Plan Introduction Groovy Grails Conclusion Références Plan de la présentation • Introduction • Groovy ‣ Du

Architecture Créer une application web

TagLib Grails

39

Créer une application web

Page 54: Groovy & Grails - dr/XPOSE2009/Groovy_and_Grails/download/... · Plan Introduction Groovy Grails Conclusion Références Plan de la présentation • Introduction • Groovy ‣ Du

Architecture Créer une application web

Plug-in

• Une liste d’extensions est disponible sur le site de Grails.

‣ Service de mail

‣ Authentification

‣ Moteur de recherche

‣ RSS

‣ Paiement en ligne

‣ etc...

• Possibilité de créer sa propre extension.

• Installation en utilisant un système repository. (Similaire à la commande apt-get de Debian)

40

Créer une application web

Page 55: Groovy & Grails - dr/XPOSE2009/Groovy_and_Grails/download/... · Plan Introduction Groovy Grails Conclusion Références Plan de la présentation • Introduction • Groovy ‣ Du

Architecture Créer une application web

Plug-in

41

Créer une application web

Page 56: Groovy & Grails - dr/XPOSE2009/Groovy_and_Grails/download/... · Plan Introduction Groovy Grails Conclusion Références Plan de la présentation • Introduction • Groovy ‣ Du

Plan Introduction Groovy Grails Conclusion Références

42

Conclusion

• Groovy

Apporte plus de flexibilité au développeur.

Gain de productivité ?

Maintenance ?

• Grails

Permet de développer rapidement une application.

Facilité à mettre en oeuvre.

Travail en équipe ?

Taille du projet ?

• Mon opinion

Conclusion

Page 57: Groovy & Grails - dr/XPOSE2009/Groovy_and_Grails/download/... · Plan Introduction Groovy Grails Conclusion Références Plan de la présentation • Introduction • Groovy ‣ Du

Plan Introduction Groovy Grails Conclusion Références

43

Références

• «Beginning Groovy and Grails», Apress (disponible sur Amazon)

• Site officiel de Groovyhttp://groovy.codehaus.org/

• Site officiel de Grailshttp://www.grails.org/

• La documentation indispensable :http://grails.org/doc/latest/

Références