Upload
lukasz-wojcik
View
1.406
Download
0
Embed Size (px)
DESCRIPTION
Citation preview
Quick bootstrap intoPlayFramework (2.1.x)
Łukasz Wójcik
First step
● Download PlayFramework from www.
playframework.com/download
● Add corresponding path to PATH
● Check if it works calling
>play help
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 ;)
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 → Main configuration file
└ routes → Routes definition
public → Public assets
└ stylesheets → CSS files
└ javascripts → Javascript files
└ images → Image files
project → sbt configuration files
└ build.properties → Marker for sbt project
└ plugins.sbt → sbt plugins including the declaration for Play itself
lib → Unmanaged libraries dependencies
logs → Standard logs folder
└ application.log → Default log file
target → Generated stuff (including generated sources out of scala views)
└ scala-2.10.0
test → source folder for unit or functional tests
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
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
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
Controllers
import play.api.mvc.Controllerimport play.api.mvc.Results._import play.api.mvc.Action
object Tasks extends Controller { def list = Action { Ok("it works") }}GET /tasks controllers.Tasks.list
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.htmland use it to form result contentviews/tasks/list.scala.html
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 athttp://www.playframework.com/documentation/2.1.x/ScalaDatabase
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
… 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 ;)
THE END
Lukasz Wójcik