GRENOBLE

              Play framework (V1) :
     simplicité et productivité au service du
                développement WEB en Java

                 Xavier NOPRE – 12/02/2013
Préambule > Sondage

    Développeur Java ?

    Qui connait Play Framework ?
    Qui utilise Play Framework ?
        V1 ?
        V2 ?


    Précision …



    Play Framework 1.2.5 - http://www.playframework.com/ - Xavier NOPRE – 12/02/2013
Introduction
    Framework WEB complet
    Pour des développeurs pour des développeurs
    Pour une approche + simple et + rapide (vs JEE ou Spring)
    Constitué de composants choisis (JPA, Hibernate, …)
    Architecture Model View Controller
    Rechargement à chaud en mode "dev"
    Présentation des erreurs "serveur" dans la page HTML
     (explication et extrait de code)
    Stateless & REST
    Basé sur des conventions
                          Productivité !
    Play Framework 1.2.5 - http://www.playframework.com/ - Xavier NOPRE – 12/02/2013
Installation & démarrage
    Téléchargement play-1.2.5.zip :
     http://downloads.typesafe.com/releases/play-1.2.4.zip
    Extraction et ajout au PATH
    Création d'une nouvelle application :
          > play new demo-human-talks
    Préparation pour Eclipse :
          > play eclipsify
    Exécution :
          > play run
    Accès :
          http://localhost:9000/
           Principes, documentation local, API



    Play Framework 1.2.5 - http://www.playframework.com/ - Xavier NOPRE – 12/02/2013
Installation & démarrage




Play Framework 1.2.5 - http://www.playframework.com/ - Xavier NOPRE – 12/02/2013
Structure
    Répertoires :
        config/  fichiers de configuration
        app/
          models/  classes pour les modèles objet (mapping JPA)
          controllers/  classes pour les contrôleurs
          views/  fichier HTML de templating pour les vues

        test/  tests unitaires et tests fonctionnels
        public/  ressources statiques (images, CSS, Javascript)




    Play Framework 1.2.5 - http://www.playframework.com/ - Xavier NOPRE – 12/02/2013
Configuration
    Répertoire "conf":
        application.conf : configuration générale de l'application :
          Nom, mode (dev / prod), langs
          Serveur HTTP et sessions
          Logs
          Base de donnée : mémoire, fichier, MySQL, PostgreSQL, JDBC, …
          JPA, memcached, proxy, mail, …
        dependencies.yml
          Dépendances (artefacts type Maven ou Ivy)
        routes
          Routages : URL  controller.action
        messages
          Internationalisation i18n (multi-langue)



    Play Framework 1.2.5 - http://www.playframework.com/ - Xavier NOPRE – 12/02/2013
Configuration > dépendances
    Artefacts disponibles sur des repositories publiques :
            conf/dependencies.yml
            # Application dependencies

            require:
                - play
                - play -> crud
                - play -> secure
                - org.apache.commons -> commons-lang3 3.1
                - org.mockito -> mockito-all 1.9.5
                - org.easytesting -> fest-assert 1.4


    Mise à jour des dépendances :
          > play dependencies

    Play Framework 1.2.5 - http://www.playframework.com/ - Xavier NOPRE – 12/02/2013
Configuration > routage URLs
    Définitions des routages entre URL et actions des
     contrôleurs, et inversement
     conf/routes
     GET     /                                    Application.index
     GET     /public/                             staticDir:public
     *       /{controller}/{action}               {controller}.{action}
     # Backbone
     GET     /bb/projects                         BBProjects.getProjects
     GET     /bb/project/{id}                     BBProjects.getProject
     POST    /bb/project                          BBProjects.createProject
     PUT     /bb/project/{id}                     BBProjects.updateProject
     DELETE /bb/project/{id}                      BBProjects.deleteProject
     POST    /bb/project/public/{id}/{isPublic}   BBProjects.postPublicProject



    Utilisation dans une vue pour faire un lien :
          <a href="@{Clients.show(client.id)}">Show</a>


    Play Framework 1.2.5 - http://www.playframework.com/ - Xavier NOPRE – 12/02/2013
Models
    Play utilise Hibernate à travers JPA (Java Persistance Api)
    Les objets sont annotés @Entity et dérivent de Model
    Model fournit des méthodes utilitaires statiques :
        post.save(), post.delete(), Post.findAll(), Post.findById(5L),
         Post.find("byTitle", "My first post").fetch() …
                          @Entity
                          public class Post extends Model {

                              public String title;
                              public String content;
                              public Date postDate;

                              @ManyToOne
                              public Author author;

                              @OneToMany
                              public List<Comment> comments;
                          }

    Play Framework 1.2.5 - http://www.playframework.com/ - Xavier NOPRE – 12/02/2013
Controllers
    Traitent les requêtes
    Dérivent de play.mvc.Controller
    Méthodes statiques
    Récupération automatique des paramètres de requête
    Binding automatique de ces paramètres en objets, selon la
     signature de la méthode
        + Dates selon différents formats
        + Uploads POST "multipart/form-data" de fichiers  File
    Actions :
        Rendu : render(), renderJSON(), renderXML(), renderBinary()
        Autre : redirect() ou appel d'une autre méthode (chainage)

    Play Framework 1.2.5 - http://www.playframework.com/ - Xavier NOPRE – 12/02/2013
Controllers > Exemple
public class Clients extends Controller {

      public static void list() {
          List<Client> clients = Client.findAll();
          render(clients);                                app/views/Clients/list.html
      }

      public static void show(Long id) {
          Client client = Client.findById(id);
          renderTemplate("Clients/showClient.html", client);
      }                                         app/views/Clients/showClient.html
      public static void create(String name) {
          Client client = new Client(name);
          client.save();
          show(client.id);                                       chainage
      }
             public static void getUnreadMessages() {
}
                 List<Message> unreadMessages = MessagesBox.unreadMessages();
                 renderJSON(unreadMessages);
             }
    Play Framework 1.2.5 - http://www.playframework.com/ - Xavier NOPRE – 12/02/2013
Vues
    Templating Groovy
        Variable : ${client.name}
        Tags : #{script 'jquery.js' /} ou #{list items:clients, as:'client' }
            Tags personnalisés dans app/views/tags
        Actions : <a href="@{Clients.show(client.id)}">Show</a>
        Textes internationalisés : &{'clientName', client.name}
        Script Java : %{ fullName = "Name : "+client.getName(); }


    Système de "decorators" (= template parent, héritage)
        Tags #{extends 'main.html' /} et #{doLayout /}



    Play Framework 1.2.5 - http://www.playframework.com/ - Xavier NOPRE – 12/02/2013
Mais encore …
    Gestion d'ID pour différents déploiements (dev, test,…)
    Tests unitaires et fonctionnels
    Gestion de modules
        Personnels
        Public, grand nombre sur http://www.playframework.com/modules
        Notamment modules Secure, CRUD, …
    Déploiement
        En "natif" avec le moteur Play
        WAR déployé sur serveur d'application (Tomcat, …)
    Macro (tags) pour le templating des vues
    …
    Play Framework 1.2.5 - http://www.playframework.com/ - Xavier NOPRE – 12/02/2013
MERCI
    Xavier NOPRE :

        Développeur Java & Web

        Formation et accompagnement en ingénierie agile (tests unitaires,
         TDD, intégration continue, industrialisation Maven, etc…)
         et technos Java-WEB



            @xnopre                xnopre.blogspot.com


    Play Framework 1.2.5 - http://www.playframework.com/ - Xavier NOPRE – 12/02/2013

Play framework - Human Talks Grenoble - 12.02.2013

  • 1.
    GRENOBLE Play framework (V1) : simplicité et productivité au service du développement WEB en Java Xavier NOPRE – 12/02/2013
  • 2.
    Préambule > Sondage  Développeur Java ?  Qui connait Play Framework ?  Qui utilise Play Framework ?  V1 ?  V2 ?  Précision … Play Framework 1.2.5 - http://www.playframework.com/ - Xavier NOPRE – 12/02/2013
  • 3.
    Introduction  Framework WEB complet  Pour des développeurs pour des développeurs  Pour une approche + simple et + rapide (vs JEE ou Spring)  Constitué de composants choisis (JPA, Hibernate, …)  Architecture Model View Controller  Rechargement à chaud en mode "dev"  Présentation des erreurs "serveur" dans la page HTML (explication et extrait de code)  Stateless & REST  Basé sur des conventions  Productivité ! Play Framework 1.2.5 - http://www.playframework.com/ - Xavier NOPRE – 12/02/2013
  • 4.
    Installation & démarrage  Téléchargement play-1.2.5.zip : http://downloads.typesafe.com/releases/play-1.2.4.zip  Extraction et ajout au PATH  Création d'une nouvelle application : > play new demo-human-talks  Préparation pour Eclipse : > play eclipsify  Exécution : > play run  Accès : http://localhost:9000/  Principes, documentation local, API Play Framework 1.2.5 - http://www.playframework.com/ - Xavier NOPRE – 12/02/2013
  • 5.
    Installation & démarrage PlayFramework 1.2.5 - http://www.playframework.com/ - Xavier NOPRE – 12/02/2013
  • 6.
    Structure  Répertoires :  config/  fichiers de configuration  app/  models/  classes pour les modèles objet (mapping JPA)  controllers/  classes pour les contrôleurs  views/  fichier HTML de templating pour les vues  test/  tests unitaires et tests fonctionnels  public/  ressources statiques (images, CSS, Javascript) Play Framework 1.2.5 - http://www.playframework.com/ - Xavier NOPRE – 12/02/2013
  • 7.
    Configuration  Répertoire "conf":  application.conf : configuration générale de l'application :  Nom, mode (dev / prod), langs  Serveur HTTP et sessions  Logs  Base de donnée : mémoire, fichier, MySQL, PostgreSQL, JDBC, …  JPA, memcached, proxy, mail, …  dependencies.yml  Dépendances (artefacts type Maven ou Ivy)  routes  Routages : URL  controller.action  messages  Internationalisation i18n (multi-langue) Play Framework 1.2.5 - http://www.playframework.com/ - Xavier NOPRE – 12/02/2013
  • 8.
    Configuration > dépendances  Artefacts disponibles sur des repositories publiques : conf/dependencies.yml # Application dependencies require: - play - play -> crud - play -> secure - org.apache.commons -> commons-lang3 3.1 - org.mockito -> mockito-all 1.9.5 - org.easytesting -> fest-assert 1.4  Mise à jour des dépendances : > play dependencies Play Framework 1.2.5 - http://www.playframework.com/ - Xavier NOPRE – 12/02/2013
  • 9.
    Configuration > routageURLs  Définitions des routages entre URL et actions des contrôleurs, et inversement conf/routes GET / Application.index GET /public/ staticDir:public * /{controller}/{action} {controller}.{action} # Backbone GET /bb/projects BBProjects.getProjects GET /bb/project/{id} BBProjects.getProject POST /bb/project BBProjects.createProject PUT /bb/project/{id} BBProjects.updateProject DELETE /bb/project/{id} BBProjects.deleteProject POST /bb/project/public/{id}/{isPublic} BBProjects.postPublicProject  Utilisation dans une vue pour faire un lien : <a href="@{Clients.show(client.id)}">Show</a> Play Framework 1.2.5 - http://www.playframework.com/ - Xavier NOPRE – 12/02/2013
  • 10.
    Models  Play utilise Hibernate à travers JPA (Java Persistance Api)  Les objets sont annotés @Entity et dérivent de Model  Model fournit des méthodes utilitaires statiques :  post.save(), post.delete(), Post.findAll(), Post.findById(5L), Post.find("byTitle", "My first post").fetch() … @Entity public class Post extends Model { public String title; public String content; public Date postDate; @ManyToOne public Author author; @OneToMany public List<Comment> comments; } Play Framework 1.2.5 - http://www.playframework.com/ - Xavier NOPRE – 12/02/2013
  • 11.
    Controllers  Traitent les requêtes  Dérivent de play.mvc.Controller  Méthodes statiques  Récupération automatique des paramètres de requête  Binding automatique de ces paramètres en objets, selon la signature de la méthode  + Dates selon différents formats  + Uploads POST "multipart/form-data" de fichiers  File  Actions :  Rendu : render(), renderJSON(), renderXML(), renderBinary()  Autre : redirect() ou appel d'une autre méthode (chainage) Play Framework 1.2.5 - http://www.playframework.com/ - Xavier NOPRE – 12/02/2013
  • 12.
    Controllers > Exemple publicclass Clients extends Controller { public static void list() { List<Client> clients = Client.findAll(); render(clients);  app/views/Clients/list.html } public static void show(Long id) { Client client = Client.findById(id); renderTemplate("Clients/showClient.html", client); }  app/views/Clients/showClient.html public static void create(String name) { Client client = new Client(name); client.save(); show(client.id);  chainage } public static void getUnreadMessages() { } List<Message> unreadMessages = MessagesBox.unreadMessages(); renderJSON(unreadMessages); } Play Framework 1.2.5 - http://www.playframework.com/ - Xavier NOPRE – 12/02/2013
  • 13.
    Vues  Templating Groovy  Variable : ${client.name}  Tags : #{script 'jquery.js' /} ou #{list items:clients, as:'client' }  Tags personnalisés dans app/views/tags  Actions : <a href="@{Clients.show(client.id)}">Show</a>  Textes internationalisés : &{'clientName', client.name}  Script Java : %{ fullName = "Name : "+client.getName(); }  Système de "decorators" (= template parent, héritage)  Tags #{extends 'main.html' /} et #{doLayout /} Play Framework 1.2.5 - http://www.playframework.com/ - Xavier NOPRE – 12/02/2013
  • 14.
    Mais encore …  Gestion d'ID pour différents déploiements (dev, test,…)  Tests unitaires et fonctionnels  Gestion de modules  Personnels  Public, grand nombre sur http://www.playframework.com/modules  Notamment modules Secure, CRUD, …  Déploiement  En "natif" avec le moteur Play  WAR déployé sur serveur d'application (Tomcat, …)  Macro (tags) pour le templating des vues  … Play Framework 1.2.5 - http://www.playframework.com/ - Xavier NOPRE – 12/02/2013
  • 15.
    MERCI  Xavier NOPRE :  Développeur Java & Web  Formation et accompagnement en ingénierie agile (tests unitaires, TDD, intégration continue, industrialisation Maven, etc…) et technos Java-WEB @xnopre xnopre.blogspot.com Play Framework 1.2.5 - http://www.playframework.com/ - Xavier NOPRE – 12/02/2013