Notes on Play framework 2.0 basics

Play Console

Command list

These commands are to be ran inside the application folder.

Command Description
play Launch console
new Create a new application
help play Basic help
run Run app in dev mode
start Run app in production mode
run/start [port_number] Specify port number. (sample usage: play debug “run 9510″)
stage Prepare your application to be run in place (“$ play clean compile stage”, “$ target/start”)
Ctrl+D Stop server
stop Use this command to stop server when your application is running in place.
compile Compile
console Enter the interactive Scala console
eclipsify Generate Eclipse configuration. You need to rerun this for some changes, such as changing the classpath. (don’t commit Eclipse configs!)
clean-all Clean all generated directories when you feel something is going wrong (ex. sbt cache is corrupted)
debug Start JPDA debug port when starting the console (default port = 9999). Run “play debug run” to use Eclipse’s “Debug As” remote debugging session.
id You can assign an ID to each framework install to manage different configurations in the same application.conf file. This can be leveraged to set configurations for dev/production environment as an example.
classpath Display classpath
projects Display the name of available projects
sbt features
Triggered execution Description
~ compile The compilation will be triggered each time you change a source file
~ run Enables triggered compilation for development server
~ test Continuously test each time you modify a source file
Direct command executions


play run

Application layout

Sample applications

Looking at the sample applications is a good way to see how things can be implemented with Play framework.

  • Hello World
    • Writing controllers and actions.
    • Routing and reverse routing.
    • Linking to public assets.
    • Using the template engine. Handling forms with validation.
  • Computer database
    • Accessing a JDBC database, using Ebean in Java and Anorm in Scala
    • Table pagination and CRUD forms
    • Integrating with a CSS framework (Twitter Bootstrap ).
  • Forms
    • Writing complex forms with validation
    • Handling forms with dynamically repeated values.
  • ZenTasks
    • Integrate authentication and security
    • Use Ajax and JavaScript reverse routing
    • Integrate with compiled assets – LESS CSS and CoffeeScript.
  • CometClock
    • Create a Comet connection
    • Use Akka actors (in the Java version)
    • Write custom Enumerators (in the Scala version).
  • WebSocket chat
    • WebSocket connections.
    • Advanced Akka usage.

Play 2.0 for Scala

  • Scala API package = play.api
  • Scala MVC = Actions, Controllers and Results

play.api.mvc.Action handling a request to generate 200 OK response (i.e. play.api.mvc.Result).

Action(parse.json) { implicit request => 
  Ok("Got request [" + request + "]")

A controller is a singleton object that generates Action values. The action generator methods can have parameters.

package controllers
import play.api.mvc._

object Application extends Controller {
  def hello(name: String) = Action { Ok("Hello " + name)
Simple Results

The results are defined by play.api.mvc.SimpleResult.

  def index = Action {
    SimpleResult (
      header = ResponseHeader(200, Map(CONTENT_TYPE -> "text/plain")), body = Enumerator("Hello world!")
  def index = Action { Ok("Hello world!")
  def index = Action {
    Redirect("/user/home", status = MOVED_PERMANENTLY)

HTTP routing

  • Routes are defined in the “conf/routes” file.
  • Supported HTTP methods: GET, POST, PUT, DELETE, HEAD
  • URI pattern can be defined using regex. The following will capture “foo/logo.png” for request like “GET /files/foo/logo.png”.
    GET /files/*name
  • If there is a conflict, the first route (in declaration order) is used.
  • You can also reverse route to generate URL from within a Scala call:
    // Redirect to /hello/Bob 
    def helloBob = Action {

More examples:

# Explicit type parameter
GET /client/:id Long)

# Extract the page parameter from the path, or fix the value for /
GET / = "home") 
GET /:page

# Pagination links, like /clients?page=3
GET /clients controllers.Clients.list(page: Int ?= 1)

Corresponding method definition:

def show(page: String) = Action {
  loadContentFromDatabase (page).map { htmlContent =>
About these ads

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s