Catalog / Play Framework Cheat Sheet
Play Framework Cheat Sheet
A concise cheat sheet for the Play Framework, covering essential concepts, configurations, routing, controllers, views, and testing.
Core Concepts & Setup
Project Setup
Create a new Play project using the command-line:
or
|
Navigate to the project directory:
|
Run the Play application in development mode:
|
Directory Structure
|
Contains core application code like controllers, models, services. |
|
Configuration files, including |
|
Static assets like CSS, JavaScript, and images. |
|
Unit and integration tests. |
|
SBT build definition file. |
Configuration (application.conf)
The
|
Routing
Routes File
The
|
Basic Route Syntax
|
Maps a GET request to |
|
Maps a POST request to |
|
Serves static assets. |
Route Parameters
|
Simple path segment parameter. |
|
Path segment parameter with type (Int, Long, UUID, etc.). |
|
Query string parameter. |
Controllers
Controller Basics (Scala)
A simple Scala controller:
|
Dependency Injection: Play uses constructor injection. The |
Controller Basics (Java)
A simple Java controller:
|
Dependency Injection: Play uses constructor injection. Annotate the constructor with |
Actions and Results
|
Represents a unit of work to be performed for an incoming request. |
|
Represents the outcome of an action, typically an HTTP response. |
|
Returns a 200 OK response with the given content. |
|
Returns a 400 Bad Request response. |
|
Returns a 303 See Other redirect to the given URL. |
Views and Templates
Templates (Twirl)
Play uses Twirl as its default template engine. Templates are located in the
|
Passing Data to Templates
In a controller (Scala):
|
Passes the string “Hello, Play!” to the |
In a controller (Java):
|
Passes the string “Hello, Play!” to the |
Template Directives
|
Used to introduce Scala expressions in a template. |
|
Used to define template parameters. |
|
Looping. |
|
Conditional statements. |
|
Define local variables. |