an introduction to play 2 framework

15
An Introduction to Play 2 Framework

Upload: ptjug

Post on 15-Jul-2015

1.643 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: An Introduction to Play 2 Framework

An Introduction to Play 2 Framework

Page 2: 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

Page 3: An Introduction to Play 2 Framework

Outline

● Play framework features● Play application anatomy● Play MVC● Form handling

Page 4: An Introduction to Play 2 Framework

What this presentation is NOT about

● Scala introduction● How to bootstrap a Play Application● How to use Play for Java

Page 5: An Introduction to Play 2 Framework

State of Java Web Frameworks

Map[String,Object]

Page 6: An Introduction to Play 2 Framework

Play Features

● Type-safety● Short feedback cycles ● Async programming

Page 7: An Introduction to Play 2 Framework

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

Page 8: An Introduction to Play 2 Framework

Play MVC

● Model: scala classes● View: twirl templates● Controllers: route file + action generator

methods

Page 9: An Introduction to Play 2 Framework

Play MVC - Controllers

● Controller○ has Action generating methods

● Action (Request =>Result)○ handles a request and generates a result

Page 10: An Introduction to Play 2 Framework

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

Page 11: An Introduction to Play 2 Framework

Play MVC - Controllers

● Route file● Reverse Routing

Page 12: An Introduction to Play 2 Framework

Play MVC - Views

● Uses a scala-based language (Twirl) for the template language

● Templates are compiled to scala functions

Page 13: An Introduction to Play 2 Framework

Play MVC - Views

● Templates being simple functions gives us:○ “taglibs”○ “master templates”

Page 14: An Introduction to Play 2 Framework

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

Page 15: An Introduction to Play 2 Framework

The END