Setting up the Play Framework

Reading Time: 2 minutes

What Play is?

Play is a high productivity Java and Scala web application framework that integrates the components and APIs you need to the modern web application. It is a Web framework whose HTTP interface is simple and powerful.

Play Requirements:

To function correctly, the Play application only needs to include the Play JAR files. Because these JAR files are published to the Maven Repository, you can build a Play project with any Java or Scala build tool.

Play requires:

  • Java versions SE 8 through SE 11, inclusive
  • sbt – we recommend the latest version

Creating a new application

Open IntelliJ enter the project name and choose the language as Scala and build the system as sbt.

New Project

It will take some time to Setup. After Build executes the given command on the terminal.

sbt new playframework/play-scala-seed.g8

It will load all the Dependencies, then It will ask you to provide the project name, organization name, and the version of both Play and Scala Respectively.

As for now, the given details are

Name: StartingWithPlay

Organization: com.knoldus

play_version: 2.8.13

scala_version: 2.13.8

Dependencies are loaded
Now we are ready to run our first project. Let’s start the server by the given below command.
sbt run

Make sure before executing this command you are in the same directory as your project. If it is not then hit the command.

cd /home/knoldus/PlayExample/./startingwithplay
where /home/knoldus/PlayExample/./startingwithplay   is the path of the template generated by play
Server starts
Boom we are good to go, at the very first time it will take some time, as it compiles and builds the libraries. Once it is started, we can hit the URL http://localhost:9000/ in the browser and we get the welcome page which is default actually.

Project Structure

The project Structure of the Play framework is a little bit different as shown in the given image.

Conclusion

In our project directory, we see four directories created by the sbt template: app/controllers, app/views, conf, and public.

  • The controllers directory is where we will store our Scala code
  • The views directory is where we’ll save our HTML templates
  • The conf directory contains our router configuration which maps a request URL to a specific class and method
  • Finally, the public directory contains the static content served by the Play Framework server
Project Structure
Play Project Structure

References

https://www.playframework.com/documentation

for more updates please visit https://blog.knoldus.com/

Leave a Reply