Introduction to Play Framework in Scala

Reading Time: 3 minutes

In this article, we will familiar with the Play Framework and figure out how we can use it to create a web application.

Play Framework is a Light Weight, Stateless, Asynchronous, Highly Scalable, Non-Blocking, and REST API-based Modern Web applications development framework. It integrates the components and APIs we need for modern web application development.

Play Framework Features

  • Hot Reloading saves development time
  • RESTful by default
  • It is based on Model-View-Controller (MVC) architecture, which is easy to learn
  • Play JSON, and Play WS (WebService) are quite good, and easy to work with

Before exploring further, please try to install Java 1.8 or a later version and the below-mentioned software.

  • scala
  • sbt
  • IntelliJ IDEA

Creating new project

Open your terminal, navigate to your location of choice, and execute the following command.

sbt new playframework/play-scala-seed.g8

The above command will prompt you for a name for the project and domain used for packages. Press enter without typing a name if you want to keep the defaults given in square brackets. Open your project or import your project using IntelliJ IDEA. If you want to keep the default name given in square brackets press enter without typing a name.

Project Structure

After compilation, the project structure looks like this

Let us understand some important sections of this project structure below:

  • app: This directory contains three packages for each component of MVC. i.e Model, View, and Controller. You can also add your own packages.
  • conf: This directory contains all our project configurations like application.conf, routes, message files for I18N, etc.
  • project: This directory contains plugins.sbt and build.properties files. plugins.sbt file contains sbt plugins used by the project and build.properties file contains sbt version.
  • public: This directory contains static assets.
  • build.sbt: You will find project metadata like name, version, and library dependencies in build.sbt at the root of the project.
  • test: This directory contains test files.

Now open conf/routes file, you will notice some endpoints.

GET     /                           controllers.HomeController.index()

Each route consists of an HTTP method, a URI path, and an action. The syntax of defining routes is the HTTP method space URI pattern space controller action. In the above route, GET is the HTTP method, / is the path and index() is the action. You can create multiple routes as per your need.

Creating Routes

Let’s create a route with a path parameter. We will

GET        /greet/:name         controllers.HomeController.greeting(name:String)

Here we have defined a route with a path parameter. It will inform the router that name is a dynamic path. The syntax of defining path parameter is :pathName

Next, we will create a greeting action inside app/controllers/HomeController.scala

def greeting(name:String) = Action { implicit request =>
    Ok("Welcome " + name)
  }

To run the application, execute the following command in the command prompt

sbt run

You will find Listening for HTTP on /0:0:0:0:0:0:0:0:9000 in the console. By default, sbt run command starts Play Application at 9000 port number. So we can access our application at http://localhost:9000/

To run the application on different port use the following command.

sbt "run 8000"

This will run the porject on 8000 port number.

When you enter http://localhost:8000/greet/knoldus in your browser:

The request life cycle

The play framework is fully stateless and only Request/Response oriented. All HTTP Requests follow the same path:

  • An HTTP Request is received by the framework.
  • The Router component tries to find the most specific route able to accept this request.
  • The corresponding action method is then invoked and code is executed.
  • If a complex view needs to be generated, a template file is rendered.
  • The result of the action method (HTTP Response code, Content) is then written as an HTTP Response.

Conclusion

In this blog, we have learned what is play framework, the benefits of using the play framework, the structure of the play project, and how the HTTP request follows a path in the play framework, etc.

References

If you find this article interesting, please check out our other articles.

Written by 

Asbin Bhadra is a Software consultant at Knoldus Inc. Knoldus does niche Reactive and Big Data product development on Scala, Spark, and Functional Java. He is recognized as a good team player, a dedicated and responsible professional, and a technology enthusiast. His current passions include utilizing the power of Scala, Akka, and Play to make Reactive systems. He has also contributed to various Akka, Play, and Scala-based templates.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading