• Aucun résultat trouvé

Groovy & Grails

N/A
N/A
Protected

Academic year: 2022

Partager "Groovy & Grails"

Copied!
57
0
0

Texte intégral

(1)

Xposé 2010 Martin Le Bas

Groovy & Grails

Langage de script basé sur Java appliqué dans un environnement JEE

1

(2)

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

Plan

(3)

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

(4)

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

Du java au Groovy

(5)

5

(6)

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

Simplification du code

Du java au Groovy

(7)

7

(8)

Du java au Groovy Le langage Groovy Groovy avancé

Le langage Groovy

• Chaines de caractères

• Constructeur

Méthodes

Closures

• Collections

Opérateurs

Le langage Groovy

(9)

Du java au Groovy Le langage Groovy Groovy avancé

9

Chaines de caractères

• Slashy string

• Multiline string

GString

Le langage Groovy

(10)

Du java au Groovy Le langage Groovy Groovy avancé

Constructeur

• Utilisation d’une Map pour initialiser un GroovyBean.

Le langage Groovy

(11)

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éthodes

Le langage Groovy

(12)

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.

Closure

Le langage Groovy

(13)

Du java au Groovy Le langage Groovy Groovy avancé

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

13

Méthode & Closure

Le langage Groovy

(14)

Du java au Groovy Le langage Groovy Groovy avancé

List

Set

Collections

Le langage Groovy

(15)

Du java au Groovy Le langage Groovy Groovy avancé

Map

Range

15

Collections

Le langage Groovy

(16)

Du java au Groovy Le langage Groovy Groovy avancé

• Possibilité de surcharger l’opérateur :

Opérateurs

Le 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)

(17)

Du java au Groovy Le langage Groovy Groovy avancé

• Nouveaux opérateurs :

Spread operator « *. »

17

Opérateurs

Le langage Groovy

(18)

Du java au Groovy Le langage Groovy Groovy avancé

• Nouveaux opérateurs : Elvis operator « ?: »

Safe navigation operator « ?. »

Field operator « .@ »

Opérateurs

Le langage Groovy

(19)

Du java au Groovy Le langage Groovy Groovy avancé

Expressions régulières

• Match operator : ==~

• Find operator : =~

19

Le langage Groovy

(20)

Du java au Groovy Le langage Groovy Groovy avancé

Expressions régulières

• Pattern operator : ~string

Le langage Groovy

(21)

Du java au Groovy Le langage Groovy Groovy avancé

Groovy avancé

• Meta Object Protocol (MOP)

• Gestion d’XML

Templates

• Tests unitaires

21

Groovy avancé

(22)

Du java au Groovy Le langage Groovy Groovy avancé

Meta Object Protocol

• Exemple simple

Groovy avancé

(23)

Du java au Groovy Le langage Groovy Groovy avancé

Meta Object Protocol

• Autre exemple : Foreach

23

Groovy avancé

(24)

Du java au Groovy Le langage Groovy Groovy avancé

XML

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

Groovy avancé

(25)

Du java au Groovy Le langage Groovy Groovy avancé

XML

• Parsing d’XML avec un XmlSlurper

25

Groovy avancé

(26)

Du java au Groovy Le langage Groovy Groovy avancé

Templates

• Utilisation du SimpleTemplateEngine

Groovy avancé

(27)

Du java au Groovy Le langage Groovy Groovy avancé

Templates

• Utilisation du SimpleTemplateEngine

26

Groovy avancé

(28)

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

Groovy avancé

(29)

28

(30)
(31)

Architecture Créer une application web

29

GRAILS

• Architecture

• Créer une application web scaffolding

controller domain

validation

services

views

taglib

plug-in

(32)

Architecture Créer une application web

Architecture

Architecture

GRAILS Framework

Java Enterprise Edition

Java Virtual Machine

Groovy Application GRAILS

(33)

Architecture Créer une application web

30

Architecture

Architecture

GRAILS Framework

Java Enterprise Edition

Java Virtual Machine

Groovy Application GRAILS

SiteMesh

(OpenSymphony)

(34)

Architecture Créer une application web

Architecture

Architecture

GRAILS Framework

Java Enterprise Edition

Java Virtual Machine

Groovy Application GRAILS

SPRING SiteMesh

(OpenSymphony)

(35)

Architecture Créer une application web

30

Architecture

Architecture

GRAILS Framework

Java Enterprise Edition

Java Virtual Machine

Groovy Application GRAILS

SPRING SiteMesh

(OpenSymphony) Hibernate

HSQLDB GORM

(36)

Architecture Créer une application web

Architecture

Architecture

GRAILS Framework

Java Enterprise Edition

Java Virtual Machine

Groovy Application GRAILS

SPRING SiteMesh

(OpenSymphony) Hibernate

HSQLDB GORM Controllers

Views (gsp) Services & Plug-in Domains

(37)

Architecture Créer une application web

30

Architecture

Architecture

GRAILS Framework

Java Enterprise Edition

Java Virtual Machine

Groovy Application GRAILS

SPRING SiteMesh

(OpenSymphony) Hibernate

HSQLDB GORM Controllers

Views (gsp) Services & Plug-in Domains

(38)

Architecture Créer une application web

Architecture

Architecture

GRAILS Framework

Java Enterprise Edition

Java Virtual Machine

Groovy Application GRAILS

SPRING SiteMesh

(OpenSymphony) Hibernate

HSQLDB GORM Controllers

Views (gsp) Services & Plug-in Domains

(39)

Architecture Créer une application web

30

Architecture

Architecture

GRAILS Framework

Java Enterprise Edition

Java Virtual Machine

Groovy Application GRAILS

SPRING SiteMesh

(OpenSymphony) Hibernate

HSQLDB GORM Controllers

Views (gsp) Services & Plug-in Domains

(40)

Architecture Créer une application web

Architecture

Architecture

GRAILS Framework

Java Enterprise Edition

Java Virtual Machine

Groovy Application GRAILS

SPRING SiteMesh

(OpenSymphony) Hibernate

HSQLDB GORM Controllers

Views (gsp) Services & Plug-in Domains

(41)

Architecture Créer une application web

30

Architecture

Architecture

GRAILS Framework

Java Enterprise Edition

Java Virtual Machine

Groovy Application GRAILS

SPRING SiteMesh

(OpenSymphony) Hibernate

HSQLDB GORM Controllers

Views (gsp) Services & Plug-in Domains

(42)

Architecture Créer une application web

Architecture

Architecture

GRAILS Framework

Java Enterprise Edition

Java Virtual Machine

Groovy Application GRAILS

SPRING SiteMesh

(OpenSymphony) Hibernate

HSQLDB GORM Controllers

Views (gsp) Services & Plug-in Domains

Gant

(43)

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

(44)

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

Créer une application web

(45)

33

(46)

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 :

Créer une application web

(47)

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...

(48)

Architecture Créer une application web

Validation

Créer une application web

(49)

Architecture Créer une application web

Validation

36

Créer une application web

(50)

Architecture Créer une application web

Services

• Scope (durée de vie) singleton

session

conversation flow

flash

request prototype

• Utilisation d’un service

Créer une application web

(51)

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

(52)

Architecture Créer une application web

TagLib Grails

Créer une application web

(53)

Architecture Créer une application web

TagLib Grails

39

Créer une application web

(54)

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.

Créer une application web

(55)

Architecture Créer une application web

Plug-in

41

Créer une application web

(56)

Plan Introduction Groovy Grails Conclusion Références

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 ?

Conclusion

(57)

Plan Introduction Groovy Grails Conclusion Références

43

Références

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

• Site officiel de Groovy

http://groovy.codehaus.org/

• Site officiel de Grails http://www.grails.org/

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

Références

Références

Documents relatifs

double cos (double a) cosinus double tan (double a) tangente double asin (double a) arc sinus double acos (double a) arc cosinus double atan (double a) arc tangente. double

Prérequis : Néant Évaluation : Contrôle continu et examen final Mentions concernées : L3 Mathématiques et Informatique Horaires hebdomadaires : 3 h C/TP. Objectifs Apprentissage

Avant de lancer le script, observer les préférences par défaut pour les points (cliquer sur l’outil Point dans la palette générale et observer la palette Aspect&amp;Couleur.. Lancer

– les applets Java (pour APPLication internET) sont des programmes lanc´es `a partir d’un fichier HTML (le langage de description de page d’Internet) par l’interm´ediaire d’un

public static boolean createTempFile(String p, String e, File dir) Création d'un chier temporaire (pas obli- gatoirement eacé à la n de l'appli- cation) dans le répertoire dir, dont

3.5 Conversions entre types tableaux ou

size Nombre de lignes à afficher selectedIndex Retourne l’option coché Liste des propriétés de l’objet option.

[r]