Top Banner
PLAY FRAMEWORK Introduction & highlights of v2.1
37
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Play framework

PLAY FRAMEWORKIntroduction & highlights of v2.1

Page 2: Play framework

WHO AM I?Andrew Skiba

I work in Tikal as the leader of Java group.

With over 50 Java experts, we meet, share, contribute, and code together on a monthly

basis.

Page 3: Play framework

YES! WE START A NEW PROJECT!

Page 4: Play framework

NEW PROJECT DILEMMA

Page 5: Play framework

WITH RISK OF ENDING UP WITH POLYGLOT PROJECT WITH MIX OF LANGUAGES AND TECHNOLOGIES - OR EVEN

REWRITING PARTS OF YOUR APPLICATION LATER...

Page 6: Play framework

WHAT HAPPENED, TWITTER?

• Bill Venners: I’m curious, and the Ruby folks will want it spelled out: Can you elaborate on what you felt the Ruby language lacked in the area of reliable, high performance code?

• Steve Jenson: One of the things that I’ve found throughout my career is the need to have long-lived processes. And Ruby, like many scripting languages, has trouble being an environment for long lived processes. But the JVM is very good at that, because it’s been optimized for that over the last ten years.

Page 7: Play framework

JAVA IS A SAFE CHOICEExcept the risk of dying while waiting for builds & redeployments, or

maintaining XML configurations...

Page 8: Play framework

CAN YOU HAVE YOUR CAKE AND EAT IT, TOO?

Page 9: Play framework

PLAY FRAMEWORKCombine them all!

Page 10: Play framework

STRONG & FASTFast turnaround - just save and refresh browser

Page 11: Play framework

REACTIVE

Text

Event driven non blocking IO

Page 12: Play framework

SCALABLEStateless, non blocking, web-friendly architecture

Page 13: Play framework

COMPLETEEasy things are easy, complicated things are possible

Page 14: Play framework

DEMO OF А NEW PROJECT

Page 15: Play framework

IT LOOKS LIKE A MAGIC!

Page 16: Play framework

SO JAVA OR SCALA?

Page 17: Play framework

FUNCTIONAL/OO BLEND

Page 18: Play framework

EXPRESSIVENESS

val qsort: List[Int] => List[Int] = { case Nil => Nil case pivot :: tail => val (smaller, rest) = tail.partition(_ < pivot) qsort(smaller) ::: pivot :: qsort(rest) }

Page 19: Play framework

JAVA COMPATIBILITY

Page 20: Play framework

UNPRECEDENTED TYPE SAFETY

Page 21: Play framework

DSLS

Page 22: Play framework

COMMUNITY

Page 23: Play framework

'NOUGH SAID!

• Charles Nutter, creator of JRuby: "Scala, it must be stated, is the current heir apparent to the Java throne. No other language on the JVM seems as capable of being a "replacement for Java" as Scala, and the momentum behind Scala is now unquestionable."

• James Strachan, creator of Groovy: "I can honestly say if someone had shown me the Programming in Scala book by by Martin Odersky, Lex Spoon & Bill Venners back in 2003 I'd probably have never created Groovy."

Page 24: Play framework

DEMO OF SCALA POWER

New JSON API with REPL

def index = Action { import play.api.libs.json._

val json = Json.obj( "status" -> "OK", "message" -> "Hello", "framework" -> Json.obj( "name" -> "Play"))

val jsonTransformer = ( __ \ 'framework).json.update( __.read[JsObject].map { o => o ++ Json.obj("version" -> "2.1.0") })

Ok( json.transform(jsonTransformer).get ) }

Page 25: Play framework

ORDER OF THINGS

Page 26: Play framework

ANATOMY OF A PLAY APPLICATION

app → Application sources └ assets → Compiled asset sources └ stylesheets → Typically LESS CSS sources └ javascripts → Typically CoffeeScript sources └ controllers → Application controllers └ models → Application business layer └ views → Templatesconf → Configurationurations files and other non-compiled resources └ 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 └ Build.scala → Application build script └ plugins.sbt → sbt plugins

Page 27: Play framework

POWERFUL URL ROUTING

# The home pageGET / controllers.Projects.index

# AuthenticationGET /login controllers.Application.loginPOST /login controllers.Application.authenticateGET /logout controllers.Application.logout # ... # Tasks GET /projects/:project/tasks controllers.Tasks.index(project: Long)POST /projects/:project/tasks controllers.Tasks.add(project: Long, folder: String)PUT /tasks/:task controllers.Tasks.update(task: Long)DELETE /tasks/:task controllers.Tasks.delete(task: Long)

# ...

# Javascript routingGET /assets/javascripts/routes controllers.Application.javascriptRoutes

# Map static resources from the /public folder to the /assets pathGET /assets/*file controllers.Assets.at(path="/public", file)

Page 28: Play framework

POWERFUL TEMPLATE ENGINE

Page 29: Play framework

UNIFIED ERROR REPORTING

Page 30: Play framework

TASTING MENU

Page 31: Play framework

SPRING WITH PLAY

@org.springframework.stereotype.Servicepublic class HelloService {

public String hello() { return "Hello world!"; }}//////////////////@org.springframework.stereotype.Controllerpublic class Application extends Controller {

@Autowired private HelloService helloService;

public Result index() { return ok(index.render(helloService.hello())); }}

Page 32: Play framework

DEMO OF REQUIREJS

Page 33: Play framework

TESTINGincluding a browser simulator!

Page 34: Play framework

DEMO OF SUBROUTES

in main project

# The home pageGET / controllers.Application.index# Include a sub-project-> /my-subproject my.subproject.Routes

in conf/my.subproject.routes

GET / my.subproject.controllers.Application.index

now just surf to /my-subproject and get called my.subproject.controllers.Application.index

Page 35: Play framework

DEMO OF REACTIVE MONGO

val query = BSONDocument("firstName" -> BSONString("Jack"))

// get a Cursor[DefaultBSONIterator] val cursor = collection.find(query) // let's enumerate this cursor and print a readable // representation of each document in the response cursor.enumerate.apply(Iteratee.foreach { doc => println("found document: " + BSONDocument.pretty(doc)) })

// or, the same with getting a list val cursor2 = collection.find(query) val futurelist = cursor2.toList futurelist.onSuccess { case list => val names = list.map(_.getAs[BSONString]("lastName").get.value) println("got names: " + names) }

Page 36: Play framework