Quick Bootstrap to PlayFramework 2.1t

9,531 views

Published on

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
9,531
On SlideShare
0
From Embeds
0
Number of Embeds
12
Actions
Shares
0
Downloads
16
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Quick Bootstrap to PlayFramework 2.1t

  1. 1. Quick bootstrap into PlayFramework (2.1.x) Łukasz Wójcik
  2. 2. First step ● Download PlayFramework from www. playframework.com/download ● Add corresponding path to PATH ● Check if it works calling >play help
  3. 3. Creating and running an Application Navigate to your projects directory, and type >play new application-name >cd applicaiton-name >play ~run Open http://localhost:9090 in your favourite browser (if your favourite browser is IE - open it in Chrome ;)
  4. 4. Generated dirs structure app → Application sources └ assets → Compiled asset sources (stylesheets and javascripts) └ controllers → Application controllers └ models → Application business layer └ views → Templates build.sbt → Application build script conf → Configurations files and other non-compiled resources (on classpath) └ application.conf └ routes → Main configuration file → Routes definition public → Public assets └ stylesheets → CSS files └ javascripts → Javascript files └ images → Image files project → sbt configuration files └ build.properties └ plugins.sbt lib logs → sbt plugins including the declaration for Play itself → Unmanaged libraries dependencies → Standard logs folder └ application.log target → Marker for sbt project → Default log file → Generated stuff (including generated sources out of scala views) └ scala-2.10.0 test → source folder for unit or functional tests
  5. 5. Basic play console commands Workflow commands: >play compile >play run >play test >play clean-all >play debug >play eclise #generates eclipse project to use with ScalaIde 3.0.2 auto triggering (as in SBT) >play ~ compile|run|test
  6. 6. Application development ● Global Application Object - can control life-time events in an app ● Controllers - do hard work around HTTP requests ● Routing - binds requested URLs with particular Controllers ● Predefined results ● Sessions and Flashes ● Async results
  7. 7. Global Application Object import play.api.GlobalSettings import play.api.mvc.RequestHeader import play.api.mvc.Handler import play.api.Logger object Global extends GlobalSettings { override def onRouteRequest(request:RequestHeader):Option [Handler] = { Logger.debug("on Request Routed" + request) super.onRouteRequest(request) } } In application.conf application.global=app.Global
  8. 8. Controllers import play.api.mvc.Controller import play.api.mvc.Results._ import play.api.mvc.Action object Tasks extends Controller { def list = Action { Ok("it works") } } GET /tasks controllers.Tasks.list
  9. 9. Views ● Are in 90% in HTML + Scala-like expression ● Are compiled to Scala Objects ● View class is actually a … method body We can create a reusable layout of a webapp: views/templates/tasks.scala.html and use it to form result content views/tasks/list.scala.html
  10. 10. Models (DAOs and Entities) We need a dependency for MySQL JDBC connector in Build.scala we add "mysql" % "mysql-connector-java" % "5.1.18", in application.conf we add (to see SQL stmnts) : db.default.logStatements=true More at http://www.playframework.com/documentation/2.1.x/ScalaDatabase
  11. 11. Models (DAOs and Entities) import play.api.Play.current //imports current application object into scope import play.api.db._ //imports DB api (facade for JDBC) //Retreiving connection val ds = DB.getDataSource() //lub val connection = DB.getConnection() //lub DB.withConnection("default") { conn => // do whatever you need with the connection } … so let’s create an Entity and DAO and do some coding
  12. 12. … after coding - time to sum up ● ● ● ● ● ● ● ● Native JSON and XML support Powerful routing syntax Web Services client (sync, async) Build in translations management Asynchronous HTTP programming Less/CoffeScript support ClosureCompiler/RequireJS support many many more ;)
  13. 13. THE END Lukasz Wójcik

×