groovy & grails - dr/xpose2009/groovy_and_grails/download/... · plan introduction groovy...
TRANSCRIPT
Xposé 2010Martin Le Bas
Groovy & GrailsLangage de script basé sur Java
appliqué dans un environnement JEE
1
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
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
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
5
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
7
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
Du java au Groovy Le langage Groovy Groovy avancé
9
Chaines de caractères
• Slashy string
• Multiline string
• GString
Le langage Groovy
Du java au Groovy Le langage Groovy Groovy avancé
10
Constructeur
• Utilisation d’une Map pour initialiser un GroovyBean.
Le langage Groovy
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
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
Du java au Groovy Le langage Groovy Groovy avancé
• Passage d’une closure à une méthode.
13
Méthode & ClosureLe langage Groovy
Du java au Groovy Le langage Groovy Groovy avancé
• List
• Set
14
CollectionsLe langage Groovy
Du java au Groovy Le langage Groovy Groovy avancé
• Map
• Range
15
CollectionsLe langage Groovy
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)
Du java au Groovy Le langage Groovy Groovy avancé
• Nouveaux opérateurs :
Spread operator « *. »
17
OpérateursLe langage Groovy
Du java au Groovy Le langage Groovy Groovy avancé
• Nouveaux opérateurs :
Elvis operator « ?: »
Safe navigation operator « ?. »
Field operator « .@ »
18
OpérateursLe langage Groovy
Du java au Groovy Le langage Groovy Groovy avancé
Expressions régulières
• Match operator : ==~
• Find operator : =~
19
Le langage Groovy
Du java au Groovy Le langage Groovy Groovy avancé
Expressions régulières
• Pattern operator : ~string
20
Le langage Groovy
Du java au Groovy Le langage Groovy Groovy avancé
Groovy avancé
• Meta Object Protocol (MOP)
• Gestion d’XML
• Templates
• Tests unitaires
21
Groovy avancé
Du java au Groovy Le langage Groovy Groovy avancé
Meta Object Protocol
• Exemple simple
22
Groovy avancé
Du java au Groovy Le langage Groovy Groovy avancé
Meta Object Protocol
• Autre exemple : Foreach
23
Groovy avancé
Du java au Groovy Le langage Groovy Groovy avancé
XML
• Génération d’XML avec un MarkupBuilder
24
Groovy avancé
Du java au Groovy Le langage Groovy Groovy avancé
XML• Parsing d’XML avec un XmlSlurper
25
Groovy avancé
Du java au Groovy Le langage Groovy Groovy avancé
Templates• Utilisation du SimpleTemplateEngine
26
Groovy avancé
Du java au Groovy Le langage Groovy Groovy avancé
Templates• Utilisation du SimpleTemplateEngine
26
Groovy avancé
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é
28
28
Architecture Créer une application web
29
GRAILS
• Architecture
• Créer une application web
scaffolding
controller
domain
validation
services
views
taglib
plug-in
Architecture Créer une application web
30
ArchitectureArchitecture
GRAILS Framework
Java Enterprise Edition
Java Virtual Machine
Groovy
Application GRAILS
Architecture Créer une application web
30
ArchitectureArchitecture
GRAILS Framework
Java Enterprise Edition
Java Virtual Machine
Groovy
Application GRAILS
SiteMesh(OpenSymphony)
Architecture Créer une application web
30
ArchitectureArchitecture
GRAILS Framework
Java Enterprise Edition
Java Virtual Machine
Groovy
Application GRAILS
SPRINGSiteMesh(OpenSymphony)
Architecture Créer une application web
30
ArchitectureArchitecture
GRAILS Framework
Java Enterprise Edition
Java Virtual Machine
Groovy
Application GRAILS
SPRINGSiteMesh(OpenSymphony) Hibernate
HSQLDB
GORM
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
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
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
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
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
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
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
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
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
33
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
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...
Architecture Créer une application web
Validation
36
Créer une application web
Architecture Créer une application web
Validation
36
Créer une application web
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
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
Architecture Créer une application web
TagLib Grails
39
Créer une application web
Architecture Créer une application web
TagLib Grails
39
Créer une application web
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
Architecture Créer une application web
Plug-in
41
Créer une application web
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
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