Play Framework

Anand Kumar Singh
Software Consultant
Knoldus Software

Play Framework

The High Velocity
Web Framework
For Java and Scala

Agenda

  • Introduction to Play

  • Why Play?

  • Installing Play

  • New application in Play

Introduction to Play

Play Framework makes it easy to build web applications with Java & Scala.

Play is based on a lightweight, stateless, web-friendly architecture.

Built on Akka, Play provides predictable and minimal resource consumption (CPU, memory, threads) for highly-scalable applications.

Features

Play was made for web developers

  • Declarative URL configuration
  • Typesafe mapping – HTTP to Scala
  • Typesafe templating
  • HTML5 ready
  • Live code changes on reload
  • Full Stack

Key Features

Simplicity

Usability

Productivity

Low boilerplating, single lines to achieve results

Code change, reload and see result, instead of long
Build deploy cycles

Developer productivity

Compilation and Exact Error

Built on Akka

Architecture

Why Play Framework?

  • Developer friendly.

  • Scale predictably.

  • Modern web & mobile.

  • Solid & fast.

  • Massive ecosystem.

  • Proven in production.

Developer friendly

  • Hit refresh workflow
  • Powerful console & build tools
  • Type safety
  • Built in testing tools
  • IDE support for Eclipse and IntelliJ IDEA

Make your changes and simply hit refresh! All you need is a browser and a text editor.

Scale predictably

  • Stateless Web Tier
  • Non-blocking I/O
  • Built on Akka
  • Real-time enabled

Underneath the covers Play uses a fully asynchronous model built on top of Akka. Combined with being stateless, Play scales simply and predictably.

Modern web & mobile

  • RESTful by default
  • Asset Compiler for CoffeeScript, LESS, etc
  • JSON is a first class citizen
  • Websockets, Comet, EventSource
  • Extensive NoSQL & Big Data Support

Play was built for needs of modern web & mobile apps.

Solid & fast

  • Code is compiled
  • Runs on the JVM
  • Java NIO via Netty

The compiler and runtime (JVM) do the heavy lifting so that your apps run super-fast and keeps running under load.

Massive ecosystem

  •  Use Maven Central libraries
  •  Numerous Play plugins
  •  Very active Play community

The ecosystem around Java is huge.

There are libraries for everything - most of which can be used in Play.

Proven in production

  • Production support by Typesafe
  • Many cloud deployment options
  • Used in mission-critical apps

From startups to enterprises, Play powers some of the most innovative web sites. Play is proven, reliable and scalable.

Installing Play

Prerequisites: To run the Play framework, you need JDK 6 or later

Step-1: Download Activator from here

Step-2: Go to the ZIP file path and UNZIP it

anand@knoldus:~/software$ unzip typesafe-activator-1.2.12-minimal.zip
Archive:  typesafe-activator-1.2.12-minimal.zip
   creating: activator-1.2.12-minimal/
  inflating: activator-1.2.12-minimal/activator.bat  
  inflating: activator-1.2.12-minimal/activator  
  inflating: activator-1.2.12-minimal/activator-launch-1.2.12.jar  
anand@knoldus:~/software$ 

    Cont...

Step-3: Set the activator to your path

anand@knoldus:~$ vi .bashrc
export TYPESAFE_ACTIVATOR=/home/anand/software/activator-1.2.10-minimal
export PATH=$PATH:$TYPESAFE_ACTIVATOR

To check
anand@knoldus:~$ which activator
/home/anand/software/activator-1.2.10-minimal/activator

NOTE: If you don’t want to setup the activator into your path then just unzip and go to the activator script path and use it.

Step-4: Check the activator command

Now your system is ready to create a new Play application.

anand@knoldus:~$ activator -help
Usage: activator <command> [options]

  Command:
  ui                 Start the Activator UI
  new [name] [template-id]  Create a new project with [name] using template [template-id]
  list-templates     Print all available template names
  -h | -help         Print this message

  Options:
  -v | -verbose      Make this runner chattier
  -d | -debug        Set sbt log level to debug
  -mem <integer>     Set memory options (default: , which is -Xms1024m -Xmx1024m -XX:PermSize=64m -XX:MaxPermSize=256m)
  -jvm-debug <port>  Turn on JVM debugging, open at the given port.

  # java version (default: java from PATH, currently java version "1.7.0_72")
  -java-home <path>  Alternate JAVA_HOME

  # jvm options and output control
  -Dkey=val          Pass -Dkey=val directly to the java runtime
  -J-X               Pass option -X directly to the java runtime
                     (-J is stripped)

  # environment variables (read from context)
  JAVA_OPTS          Environment variable, if unset uses ""
  SBT_OPTS           Environment variable, if unset uses ""
  ACTIVATOR_OPTS     Environment variable, if unset uses ""

In the case of duplicated or conflicting options, the order above
shows precedence: environment variables lowest, command line options highest.

New application

anand@knoldus:~/projects/examples$ activator new into-to-play play-scala

Fetching the latest list of templates...

OK, application "into-to-play" is being created using the "play-scala" template.

To run "into-to-play" from the command line, "cd into-to-play" then:
/home/anand/projects/examples/into-to-play/activator run

To run the test for "into-to-play" from the command line, "cd into-to-play" then:
/home/anand/projects/examples/into-to-play/activator test

To run the Activator UI for "into-to-play" from the command line, "cd into-to-play" then:
/home/anand/projects/examples/into-to-play/activator ui

anand@knoldus:~/projects/examples$ 

$ activator new into-to-play play-scala

Top level directories

app — Application source code
conf — Configuration files and data
project — Project build scripts
public — Publicly accessible static files
test — Automated tests
project — sbt build definitions
target — everything generated by build  system

Anatomy of Play

app                      → Application sources
 └ assets                → Compiled asset sources
    └ stylesheets        → Typically LESS CSS sources
    └ javascripts        → Typically CoffeeScript sources
 └ 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
 └ scala-2.10.0            
    └ cache              
    └ classes            → Compiled class files
    └ classes_managed    → Managed class files (templates, ...)
    └ resource_managed   → Managed resources (less, ...)
    └ src_managed        → Generated sources (templates, ...)
test                     → source folder for unit or functional tests

The conf/routes file

HTTP Method    URL Path             Controller Action Mapping

# Routes          
# This file defines all application routes (Higher priority routes first)
# ~~~~                                                                                     
                                             
# Home page
GET             /                   controllers.Application.index


# Map static resources from the /public folder to the /assets URL path
GET             /assets/*file       controllers.Assets.at(path="/public", file)

Useful commands

activator : To launch play console .

run       : To run current application in development mode .

compile   : To compile application without running the server .

clean     : To clean current application without running server.

console   : To enter the interactive Scala console, which allows you to test your code interactively

help      : To get basic help about the available command .

Batch Mode Command
activator clean compile run

Note : You can also use “sbt” instead of “activator” command

Run the application

$ cd into-to-play

$ activator

anand@knoldus:~/projects/examples$ cd into-to-play/
anand@knoldus:~/projects/examples/into-to-play$ activator
[info] Loading project definition from /home/anand/projects/examples/into-to-play/project
[info] Updating {file:/home/anand/projects/examples/into-to-play/project/}into-to-play-build...
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] Done updating.
[info] Set current project to into-to-play (in build file:/home/anand/projects/examples/into-to-play/)
[into-to-play] $ 

To launch the play application , change the directory of your project and run “activator”

Cont...

$ run

[into-to-play] $ run
[info] Updating {file:/home/anand/projects/examples/into-to-play/}root...
[info] Resolving jline#jline;2.11 ...
[info] Done updating.

--- (Running the application, auto-reloading is enabled) ---

[info] play - Listening for HTTP on /0:0:0:0:0:0:0:0:9000

(Server started, use Ctrl+D to stop and go back to the console...)

To run the current application in development mode,

use the run command:

Cont...

$ run 8080

[into-to-play] $ run 8080

--- (Running the application, auto-reloading is enabled) ---

[info] play - Listening for HTTP on /0:0:0:0:0:0:0:0:8080

(Server started, use Ctrl+D to stop and go back to the console...)


Just type run to start the server on port 9000, but if that port is already taken by another application (such as Eclipse), specify a different port for it to run on:

Cont...

When you see the “Server started” message, the Play server is running, and you can now access your application from a browser. Because  started the server on port 8080,  access the http://localhost:8080/ URL in  browser  and you see

References

  •  Getting started with Play Framework

  •  Installing Play

  •  Creating a new application

Made with Slides.com