Playing With Scala - Hello, World!

Scala is a language stuck between two worlds. The FP world attracts many talents, but the OOP one is often where people start. My “taming cats” posts covered cats for the first group. Bellow is the start of a few posts on the Play Framework for the second.

Play Framework

The Play Framework is one of the simplest ways to create web applications in Scala. The MVC framework contains routing, JSON formatting, forms and validation, sessions, database interactions (Anorm, Evolutions), templating, dependency injection and much more.

Furthermore, the Scala knowledge to write controllers is low. At most, understanding Future is helpful, but not required.

Let’s prove that with a simple example.

Giter8

Play Framework has a giter8 template to get started.

> sbt new playframework/play-scala-seed.g8
[info] Loading global plugins from [HOME]/.sbt/1.0/plugins
[info] Set current project to code (in build file:[PWD])
[info] Set current project to code (in build file:[PWD])

This template generates a Play Scala project

name [play-scala-seed]:
organization [com.example]:

Template applied in [PWD]/./play-scala-seed

> cd play-scala-seed

The template contains a working application with a route, a controller, a view, and tests for all those.

sbt run

Once the server has started, localhost:9000 should display Play’s welcome message. From here, implementing “Hello, World!” is easy.

Hello, World!

The first step is to add a new route in the /conf/routes file. The line starts with an HTTP verb, a URI, and the method to call. 

GET /hello-world controllers.HomeController.helloWorld

The new endpoint will throw an error. 

value helloWorld is not a member of controllers.HomeController

Implementing the missing method will fix the issue. The HomeController is in the /app/controllers/HomeController.scala file. The following helloWorld definition will do the trick.

def helloWorld = Action(Ok("Hello, World!"))

Opening localhost:9000/hello-world should now display “Hello, World!”.


Play Framework has a great template. It simplifies the getting started process like the “Hello, World!” endpoint above. It isn’t impressive but gives a stable foundation to build on. 

With tradition out of the way, the next post will be on ReST.