an introduction to play 2 framework
TRANSCRIPT
An Introduction to Play 2 Framework
Who am I?
● João Ferreira (@jtjeferreira)● 5 years professional experience in backend
& frontend development with Java, C# and Javascript
● Currently working @ E.Near, developing a scala applications
Outline
● Play framework features● Play application anatomy● Play MVC● Form handling
What this presentation is NOT about
● Scala introduction● How to bootstrap a Play Application● How to use Play for Java
State of Java Web Frameworks
Map[String,Object]
Play Features
● Type-safety● Short feedback cycles ● Async programming
Play application anatomy
app → Application sources └ assets → Compiled asset sources └ stylesheets → Typically LESS CSS sources └ javascripts → Typically CoffeeScript sources └ controllers → Application controllers └ models → Application business layer └ views → Templatesbuild.sbt → Application build scriptconf → Configurations files and other non-compiled resources (on classpath) └ application.conf → Main configuration file └ routes → Routes definitionpublic → Public assets └ stylesheets → CSS files └ javascripts → Javascript files └ images → Image filesproject → sbt configuration files └ build.properties → Marker for sbt project └ plugins.sbt → sbt plugins including the declaration for Play itselflib → Unmanaged libraries dependenciestest → source folder for unit or functional tests
Play MVC
● Model: scala classes● View: twirl templates● Controllers: route file + action generator
methods
Play MVC - Controllers
● Controller○ has Action generating methods
● Action (Request =>Result)○ handles a request and generates a result
Play MVC - Controllersval ok = Ok("Hello world!")val notFound = NotFoundval pageNotFound = NotFound(<h1>Page not found</h1>)val badRequest = BadRequest(views.html.form(formWithErrors))val oops = InternalServerError("Oops")val anyStatus = Status(488)("Strange response type")
Play MVC - Controllers
● Route file● Reverse Routing
Play MVC - Views
● Uses a scala-based language (Twirl) for the template language
● Templates are compiled to scala functions
Play MVC - Views
● Templates being simple functions gives us:○ “taglibs”○ “master templates”
Forms support- login use case
Login FormuserLoginForm: Form[UserLogin]
user: textpass: text
POST /loginuser=apass=b
Do stuff and replyLogin Success
userLogin:UserLogin
user: apass: b
userLoginForm.bindFromRequest
userLoginForm.fold( errorFunc, successFunc )
login.scala.html
loginSuccess.scala.html
The END