Small introduction to Play framework
Kamil Lolo
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.
in Idea
Play has built-in HTTP router.
His configuration locates in conf/routes file.
Configuration allows you for:
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.
The Flash scope works exactly like the Session, but with two differences:
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!
}