play framework
DESCRIPTION
TRANSCRIPT
Play! frameworkPlay! framework
Sẩm Bảo Chung Web development with Play!
Agile web development.
Created by Guillaume Bort 2007
AgendaAgenda
OverviewMain 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 enviromentDiscuss
Sẩm Bảo Chung Web development with Play!
Play OverviewPlay OverviewPure 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 ConceptMain 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 MVCPlay architecture MVCThe Play framework is fully stateless and only request/response-oriented
1.An HTTP Request is received by the framework.
2.The Router component tries to find the most specific route able to accept this request.
3.The corresponding action method is then invoked.
4.The application code is executed.5.If a complex view needs to be generated, a template file is rendered.
6.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 fileRoutes 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 xGET / u s e r /{ username } Ap p l i c a t i o n . showUserPOST / u s e r Ap p l i c a t i o n . c r e a t eUs e rDELETE / u s e r /{ username } Ap p l i c a t i o n . d e l e t eUs e rGET / p u b l i c s t a t i c D i r : p u b l i c
Sẩm Bảo Chung Web development with Play
ModelModel
A simple user
@Entitypublic class User extends Model{
@Idpublic long id;
@Required@Column(unique = true)public String username;
@Required@Emailpublic String email;
}
Sẩm Bảo Chung Web development with Play
ControllerController
UserControllerpublic 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
ViewView
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 templatesPlay 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 Fix the bug and hit ReloadReload
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
JobJob 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") */@OnApplicationStartpublic 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 startGetting start
Using the play commandWhen 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 startGetting start
Use the new command to create a new application. # play new myApp
Sẩm Bảo Chung Web development with Play
Getting startGetting start
You can start with command# play run myApp
Sẩm Bảo Chung Web development with Play
ModulesModulesSome module on http://www.playframework.org/modulesWith:ScalaGoogle App EnginePDF GenerationSASS and SCSSGoogle Web ToolkitMongoDBSimple searchObjectifyTo 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