Kick-start to a Play Framework

Reading Time: 3 minutes

Hello everyone, welcome to my blog πŸ™‚

In this blog we’ll get familiar with some basic concepts of play framework.

Play is a full-stack framework with all of the components you need to build a Web Application or a REST service, including: an integrated HTTP server, form handling, Cross-Site Request Forgery (CSRF) protection, a powerful routing mechanism, I18n support, and many more.

Why Play?

  • Its Model-View-Controller (MVC) architecture is familiar and easy to learn.
  • Direct support of common web development tasks and hot reloading saves precious development time.
  • It embraces a fully reactive programming model through the use of futures for asynchronous programming, work stealing for maximizing available threads, and Akka for distribution of work.
  • Play provides an enhanced development experience (support for routes, templates compilation and auto-reloading) when using the sbt or Gradle build tools.

Let’s dig deeper into play by creating a new project using the following command:

sbt new playframework/play-scala-seed.g8
Directory Structure

Open your project using a basic code editor or import your project in IntelliJ or Eclipse by using ScalaIDE.

After the first successful compilation, the project structure looks like this:

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
dist                     β†’ Arbitrary files to be included in your projects distribution
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                     β†’ Logs folder
 β”” application.log       β†’ Default log file
target                   β†’ Generated stuff
 β”” resolution-cache      β†’ Info about dependencies
 β”” scala-2.11
    β”” api                β†’ Generated API docs
    β”” classes            β†’ Compiled class files
    β”” routes             β†’ Sources generated from routes
    β”” twirl              β†’ Sources generated from templates
 β”” universal             β†’ Application packaging
 β”” web                   β†’ Compiled web assets
test                     β†’ source folder for unit or functional tests

This is the standard layout of the play application.

The app/ directory

App directory contains three packages for each component of MVC:

  • models
  • views
  • controllers

You can add your own packages, for example, an app/utils package.

The conf/ directory

The conf directory contains the application’s configuration files. There are two main configuration files:

  • application.conf, the main configuration file for the application, which contains configuration parameters
  • routes, the routes definition file.

The public/ directory

Public directories are static assets that are split into 3 categories :

  • images
  • CSS stylesheets
  • JavaScript files

There are served directly by the Web server.

The build.sbt file

You’ll find your project’s main build declarations in build.sbt at the root of the project.

The project/ directory

The project directory contains the sbt build definitions:

  • plugins.sbt defines sbt plugins used by this project.
  • build.properties contains the sbt version to use to build your app.

Now let’s run our project and see how our play application works.
Execute the following command in command in command prompt:

sbt run

Now when you enter http://localhost:9000/ in your browser:

  • The browser requests the root / URI from the HTTP server using the GET method.
  • The Play internal HTTP Server receives the request.
  • Play resolves the request using the routes file, which maps URIs to controller action methods.
  • The action method renders the index page, using Twirl templates.
  • The HTTP server returns the response as an HTML page.

When you look at the implementation you’ll notice that:

  • The routes file maps the request to the controller method.
  • The controller action method defines how to handle a request to the root URI.
  • The Twirl template are called by the action methods to render the HTML markup.

To view the route that maps the browser request to the controller method, open the conf/routes file. A route consists of an HTTP method, a path, and an action. This control over the URL schema makes it easy to design clean, human-readable, bookmarkable URLs. The following line maps a GET request for the root URL / to the index action in Application:

# Routes
GET         /                        controllers.Application.index

The controller class includes the index action method, as shown below. This is a very simple action method that generate an HTML page from the index.scala.html Twirl template file.

def index = Action {
  Ok(views.html.index())
}

Open app/views/index.scala.html with your text editor. The main directive in this file calls the main template main.scala.html with the string Welcome to generate the page. You can open app/views/main.scala.html to see how a String parameter sets the page title.

So these are some of the basics to kick start with the play framework.
Hope this blog made your learning easier… Happy learning πŸ™‚

In this blog we learnt about play as a web framework, the benefits of developing web application using play web framework, the standard layout of play project and how a play application handles the HTTP requests.

In my next blog we’ll learn more by creating a simple play application.

Written by 

Megha Jatana is the Software Consultant at Knoldus Software LLP. She has done MCA from Lal Bahadur Shastri Institute of Management, New Delhi. She has good knowledge of C, C++, Visual Basic, C#, Java, Scala, MongoDB, Akka, Kafka, Firebase, Cloud Firestore, HTML. She always tries to explore new things be it related to IT or in daily life. Her hobbies include dancing and adventure.