Let's Play

Small introduction to Play framework

Kamil Lolo

What is Play

framework?

Play is an open source web application framework, written in Scala and also usable from e.g. Java (Play includes a Java wrapper API in latest version), which follows the model–view–controller (MVC) architectural pattern. It aims to optimize developer productivity by using convention over configuration, hot code reloading and display of errors in the browser.

What it has?

  • implementation of MVC
  • custom server and router
  • Java compiler
  • asynchronous streaming library called Akka Streams
  • custom view engine 
  • support for DI out of box

What it DOESN NOT has?

  • session (exist session in browser and flash)

How to START?

ACTIVATOR

generate project 

in Idea

Title Text

HTTP Routing

Play has built-in HTTP router.

His configuration locates in conf/routes file. 

 

Configuration allows you for:

  • using method such get, post, delete etc.
  • set params in service
  • connect path with method
  • using regular expression
  •  

Title Text

Session

Session

It’s important to understand that Session data are not stored in the server but are added to each subsequent HTTP Request, using Cookies. This means that the data size is very limited (up to 4 KB) and that you can only store string values.

FLASH SCOPE

The Flash scope works exactly like the Session, but with two differences:

  • data are kept for only one request
  • the Flash cookie is not signed, making it possible for the user to modify it.

Session example

public Result login() {
    session("connected", "user@gmail.com");
    return ok("Welcome!");
}

public Result logout() {
    session().remove("connected");
    return ok("Bye");
}

public Result index() {
    String user = session("connected");
    if(user != null) {
        return ok("Hello " + user);
    } else {
        return unauthorized("Oops, you are not connected");
    }
}

Flash example

public Result save() {
    flash("success", "The item has been created");
    return redirect("/home");
}

...and in the template:
@if(flash.containsKey("success")) {
  @flash.get("success")
} else {
  Welcome!
}

Twirl

 

Twirl


Title Text

and it's THE END

BOUNCH of URLS

  • Bullet One
  • Bullet Two
  • Bullet Three
Made with Slides.com