Play! framework
                  Agile web development.


                Created by Guillaume Bort 2007




Sẩm Bảo Chung     Web development with Play!
Agenda
      Overview
      Main Concept
            Five cool things you can do with play
            Fix the bug and hit Reload
            Simple stateless MVC architecture
            Efficient template engine
            Test driven development
            Job
      Play enviroment
      Discuss




Sẩm Bảo Chung        Web development with Play!
Play Overview
      Pure   Java
      More    productive Java environment!
      Targets   Restful architectures
      No   compile, deploy, restart cycle - Fix the bug and hit
      reload!
      Clean    template system - using Groovy, Scala template
      Exact    errors (including line numbers, even for templates)
      Lots   of built-in features for fast development
      Starts   fast, runs fast
      Extensible    by modules


Sẩm Bảo Chung          Web development with Play
Main Concept

      Simple   stateless MVC architecture
      Five   cool things you can do with play
      Fix   the bug and hit Reload
      Efficient   template engine
      JPA

      Test   driven development
      Job




Sẩm Bảo Chung          Web development with Play!
Play architecture MVC
                            The Play framework is fully stateless and
                            only request/response-oriented
                            4.AnHTTP Request is received by the
                            framework.
                            6.TheRouter component tries to find the
                            most specific route able to accept this
                            request.
                            8.The corresponding action method is
                            then invoked.
                            10.The application code is executed.
                            11.If a complex view needs to be
                            generated, a template file is rendered.
                            13.The result of the action method (HTTP
                            Response code, Content) is then written
                            as an HTTP Response.




Sẩm Bảo Chung   Web development with Play!
Routes file
      A routes file help play controller routine
      the request

      GET / Ap p l i c a t i o n . i n d e x
      GET / u s e r /{ username } Ap p l i c a t i o n . showUser
      POST / u s e r                  Ap p l i c a t i o n . c r e a t
      eUs e r
      DELETE / u s e r /{ username }           Ap p l i c a t i o n . d e
      l e t eUs e r
      GET / p u b l i c       staticDir:public



Sẩm Bảo Chung         Web development with Play
Model
      A   simple user

      @Entity
      public class User extends Model{

           @Id
           public long id;

           @Required
           @Column(unique = true)
           public String username;

           @Required
           @Email
           public String email;
      }


Sẩm Bảo Chung        Web development with Play
Controller
      UserController
      public class Application extends Controller {
          public static void index() {
          List<User> users = User.findAll();
          render(users);
          }

          public static void showUser(String username) {
          User user = User.find("byUsername",
             username).first();
          notFoundIfNull(user);
          render(user);
      }


Sẩm Bảo Chung        Web development with Play
View
      List users
      (app/views/Application/index.html)
         #{ extends ’main .html ’ /}
         #{ set title :’Index ’ /}
         <ul >
         #{ list items :users , as:’user ’}
            <li >
            #{a @Application . showUser ( user .
                username )}
                ${ user . username }
            #{/ a}
            with email address ${ user . email }
            </li >
         #{/ list }
         </ul >

Sẩm Bảo Chung         Web development with Play
Five cool things of Play!
      Bind   an HTTP parameter to a Java method parameter




      Redirect   to an action by calling the corresponding Java
      method
             render(…) , renderText(…), renderXML(…),
      renderJSON(…), renderBinary(…), redirect(…)



Sẩm Bảo Chung          Web development with Play
Five cool things of Play!
      Don’t  Repeat Yourself when passing Java objects to
      templates
      Play will automatically start the JPA Entity Manager
      using Hibernate and magically synchronize it when code
      is reloaded.




Sẩm Bảo Chung      Web development with Play
Five cool things of Play!
      Straightforward   file upload management

      #{form @uploadPhoto(), enctype:'multipart/form-
      data'} <input type="text" name="title" /> <input
      type="file" id="photo" name="photo" /> <input
      type="submit" value="Send it..." /> #{/}

      public static void uploadPhoto(String title, File photo)
      { ...
      }




Sẩm Bảo Chung        Web development with Play
Fix the bug and hit
    Reload
      Whenever an error occurs, the framework makes its best
      effort to identify and show you the problem




Sẩm Bảo Chung      Web development with Play
Job
         Doing the right thing at the right time
          •   Scheduled jobs (housekeeping)
          •   Bootstrap jobs (initial data providing)
          •   Suspendable requests (rendering a PDF
              report without blocking the
          •   connection thread pool)
      /* @Every("1h") */
      @OnApplicationStart
      public class LoadDataJob extends Job {
          public void doJob() {
          /* .. do whatever you want */
          }
      }



Sẩm Bảo Chung        Web development with Play
Test driven development
      The integrated test runner makes it easy for you do test-
                                          driven development.




Sẩm Bảo Chung      Web development with Play
Getting start
      Using the play command
      When the framework is correctly installed, open a shell
      and execute play.
      $ play
      You should see the play default message.




Sẩm Bảo Chung       Web development with Play
Getting start
      Use the new command to create a new application.
      # play new myApp




Sẩm Bảo Chung      Web development with Play
Getting start
      You can start with command
      # play run myApp




Sẩm Bảo Chung      Web development with Play
Modules
      Some module on
      http://www.playframework.org/modules
      With:
      Scala
      Google App Engine
      PDF Generation
      SASS and SCSS
      Google Web Toolkit
      MongoDB
      Simple search
      Objectify
      To install locally these modules use the install command:
      play install gae-{version}
      To add this module as dependency of your application, add it
      to the dependencies.yml file:
      require: - play -> gae {version}.



Sẩm Bảo Chung        Web development with Play

Play framework

  • 1.
    Play! framework Agile web development. Created by Guillaume Bort 2007 Sẩm Bảo Chung Web development with Play!
  • 2.
    Agenda Overview Main Concept  Five cool things you can do with play  Fix the bug and hit Reload  Simple stateless MVC architecture  Efficient template engine  Test driven development  Job Play enviroment Discuss Sẩm Bảo Chung Web development with Play!
  • 3.
    Play Overview Pure Java More productive Java environment! Targets Restful architectures No compile, deploy, restart cycle - Fix the bug and hit reload! Clean template system - using Groovy, Scala template Exact errors (including line numbers, even for templates) Lots of built-in features for fast development Starts fast, runs fast Extensible by modules Sẩm Bảo Chung Web development with Play
  • 4.
    Main Concept Simple stateless MVC architecture Five cool things you can do with play Fix the bug and hit Reload Efficient template engine JPA Test driven development Job Sẩm Bảo Chung Web development with Play!
  • 5.
    Play architecture MVC The Play framework is fully stateless and only request/response-oriented 4.AnHTTP Request is received by the framework. 6.TheRouter component tries to find the most specific route able to accept this request. 8.The corresponding action method is then invoked. 10.The application code is executed. 11.If a complex view needs to be generated, a template file is rendered. 13.The result of the action method (HTTP Response code, Content) is then written as an HTTP Response. Sẩm Bảo Chung Web development with Play!
  • 6.
    Routes file A routes file help play controller routine the request GET / Ap p l i c a t i o n . i n d e x GET / u s e r /{ username } Ap p l i c a t i o n . showUser POST / u s e r Ap p l i c a t i o n . c r e a t eUs e r DELETE / u s e r /{ username } Ap p l i c a t i o n . d e l e t eUs e r GET / p u b l i c staticDir:public Sẩm Bảo Chung Web development with Play
  • 7.
    Model A simple user @Entity public class User extends Model{ @Id public long id; @Required @Column(unique = true) public String username; @Required @Email public String email; } Sẩm Bảo Chung Web development with Play
  • 8.
    Controller UserController public class Application extends Controller { public static void index() { List<User> users = User.findAll(); render(users); } public static void showUser(String username) { User user = User.find("byUsername", username).first(); notFoundIfNull(user); render(user); } Sẩm Bảo Chung Web development with Play
  • 9.
    View List users (app/views/Application/index.html) #{ extends ’main .html ’ /} #{ set title :’Index ’ /} <ul > #{ list items :users , as:’user ’} <li > #{a @Application . showUser ( user . username )} ${ user . username } #{/ a} with email address ${ user . email } </li > #{/ list } </ul > Sẩm Bảo Chung Web development with Play
  • 10.
    Five cool thingsof Play! Bind an HTTP parameter to a Java method parameter Redirect to an action by calling the corresponding Java method render(…) , renderText(…), renderXML(…), renderJSON(…), renderBinary(…), redirect(…) Sẩm Bảo Chung Web development with Play
  • 11.
    Five cool thingsof Play! Don’t Repeat Yourself when passing Java objects to templates Play will automatically start the JPA Entity Manager using Hibernate and magically synchronize it when code is reloaded. Sẩm Bảo Chung Web development with Play
  • 12.
    Five cool thingsof Play! Straightforward file upload management #{form @uploadPhoto(), enctype:'multipart/form- data'} <input type="text" name="title" /> <input type="file" id="photo" name="photo" /> <input type="submit" value="Send it..." /> #{/} public static void uploadPhoto(String title, File photo) { ... } Sẩm Bảo Chung Web development with Play
  • 13.
    Fix the bugand hit Reload Whenever an error occurs, the framework makes its best effort to identify and show you the problem Sẩm Bảo Chung Web development with Play
  • 14.
    Job  Doing the right thing at the right time • Scheduled jobs (housekeeping) • Bootstrap jobs (initial data providing) • Suspendable requests (rendering a PDF report without blocking the • connection thread pool) /* @Every("1h") */ @OnApplicationStart public class LoadDataJob extends Job { public void doJob() { /* .. do whatever you want */ } } Sẩm Bảo Chung Web development with Play
  • 15.
    Test driven development The integrated test runner makes it easy for you do test- driven development. Sẩm Bảo Chung Web development with Play
  • 16.
    Getting start Using the play command When the framework is correctly installed, open a shell and execute play. $ play You should see the play default message. Sẩm Bảo Chung Web development with Play
  • 17.
    Getting start Use the new command to create a new application. # play new myApp Sẩm Bảo Chung Web development with Play
  • 18.
    Getting start You can start with command # play run myApp Sẩm Bảo Chung Web development with Play
  • 19.
    Modules Some module on http://www.playframework.org/modules With: Scala Google App Engine PDF Generation SASS and SCSS Google Web Toolkit MongoDB Simple search Objectify To install locally these modules use the install command: play install gae-{version} To add this module as dependency of your application, add it to the dependencies.yml file: require: - play -> gae {version}. Sẩm Bảo Chung Web development with Play