13
Quick bootstrap into PlayFramework (2.1.x) Łukasz Wójcik

Quick Bootstrap to PlayFramework 2.1t

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Quick Bootstrap to PlayFramework 2.1t

Quick bootstrap intoPlayFramework (2.1.x)

Łukasz Wójcik

Page 2: Quick Bootstrap to PlayFramework 2.1t

First step

● Download PlayFramework from www.

playframework.com/download

● Add corresponding path to PATH

● Check if it works calling

>play help

Page 3: Quick Bootstrap to PlayFramework 2.1t

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

Page 4: Quick Bootstrap to PlayFramework 2.1t

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

Page 5: Quick Bootstrap to PlayFramework 2.1t

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

Page 6: Quick Bootstrap to PlayFramework 2.1t

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

Page 7: Quick Bootstrap to PlayFramework 2.1t

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

Page 8: Quick Bootstrap to PlayFramework 2.1t

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

Page 9: Quick Bootstrap to PlayFramework 2.1t

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

Page 10: Quick Bootstrap to PlayFramework 2.1t

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

Page 11: Quick Bootstrap to PlayFramework 2.1t

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

Page 12: Quick Bootstrap to PlayFramework 2.1t

… 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 ;)

Page 13: Quick Bootstrap to PlayFramework 2.1t

THE END

Lukasz Wójcik