Community contributed extensions

Your first Play Scala application

A very simple tutorial, that will just help you to discover Play Scala with the famous ‘Hello World’ example.

Prerequisites

First of all, make sure that you have a working Play installation, with the Scala module correctly enabled.

As we will use the command line a lot, it’s better to use a Unix-like OS. If you run a Windows system, it will also work fine; you’ll just have to type a few commands in the command prompt.

You will of course need a text editor. As in Java, Play Scala manages the compilation and the deployment process itself.

Project creation

Now that Play is correctly installed, it’s time to create the hello world application. Creating a Play application is pretty easy and fully managed by the Play command line utility. That allows for standard project layouts between all Play applications.

Open a new command line and type:

$ play new helloworld --with scala

It will prompt you for the application full name. Type Hello world.

$ play new helloworld --with scala
~        _            _ 
~  _ __ | | __ _ _  _| |
~ | '_ \| |/ _' | || |_|
~ |  __/|_|\____|\__ (_)
~ |_|            |__/   
~
~ play! 1.2, http://www.playframework.org
~
~ The new application will be created in ~/myScalaApp
~ What is the application name? [helloworld] Hello world
~
~ Resolving dependencies using ~/helloworld/conf/dependencies.yml,
~
~ 	play->scala 0.9 (from playLocalModules)
~
~ Installing resolved dependencies,
~
~ 	modules/scala-0.9 -> ~/play/1.2/modules/scala-0.9
~
~ OK, the application is created.
~ Start it with : play run helloworld
~ Have fun!
~

The play new command creates a new directory helloworld/ and populates it with a series of files and directories, the most important being:

app/ contains the application’s core. It contains all the application *.scala files. You see that the default is a single controllers.scala file. You can of course use more files and organize them in several folders for more complex applications. Also the app/views directory is where template files live. A template file is generally a simple text, xml or html file with dynamic placeholders.

conf/ contains all the application’s configuration files, especially the main application.conf file, the routes definition files and the messages files used for internationalization.

lib/ contains all optional Scala libraries packaged as standard .jar files.

public/ contains all the publicly available resources, which includes JavaScript, stylesheets and images directories.

test/ contains all the application tests. Tests are either written either as ScalaTest tests or as Selenium tests.

Because Play uses UTF-8 as single encoding, it’s very important that all text files hosted in these directories are encoded using this charset. Make sure to configure your text editor accordingly.

Now if you’re a seasoned Scala developer, you may wonder where all the .class files go. The answer is nowhere: Play doesn’t use any class files but reads the Scala source files directly.

That allows two very important things in the development process. The first one is that Play will detect changes you make to any Scala source file and automatically reload them at runtime. The second is that when an error occurs, Play will create better error reports showing you the exact source code.

Running the application

We can now test the newly-created application. Just return to the command line, go to the newly-created helloworld/ directory and type play run. Play will now load the application and start a Web server on port 9000.

You can see the new application by opening a browser to http://localhost:9000. A new application has a standard welcome page that just tells you that it was successfully created.

Let’s see how the new application can display this page.

The main entry point of your application is the conf/routes file. This file defines all of the application’s accessible URLs. If you open the generated routes file you will see this first ‘route’:

GET		/			Application.index

That simply tells Play that when the web server receives a GET request for the / path, it must call the Application.index Scala method. In this case, Application.index is a shortcut for controllers.Application.index, because the controllers package is implicit.

When you create standalone Scala applications you generally create a singleton object with a single entry point defined by a method such as:

object MyScalaApp {
    def main(args: Array[String]) {
        ...
    }
}

A Play application has several entry points, one for each URL. We call these methods action methods. Action methods are defined in several objects that we call controllers.

Let’s see how the controllers.Application controller looks like. Open the helloworld/app/controllers.scala source file:

package controllers
 
import play.mvc._
 
object Application Controller {
 
    def index = Template
 
}

You see that controller objects extend the play.mvc.Controller class. This class provides all useful methods for controllers. Also we have imported the play.mvc._ package that contain everything needed to write controllers, like the Template value.

The index action is defined as an, obejct method. This is how action methods are defined. They always return a value that is interpreted by the framework as the response value.

The default index action is simple: it returns the Template value which tells Play to render a template. Using a template is the most common way (but not the only one) to generate the HTTP response.

Templates are simple text files that live in the /app/views directory. Because we didn’t specify a template, the default one for this action will be used: Application/index.html

To see what the template looks like, open the helloworld/app/views/Application/index.html file:

#{extends 'main.html' /}
#{set title:'Home' /}
 
#{welcome /}

The template content seems pretty light. In fact, all you see are Play tags. Play tags are very close to JSP taglib. This is the #{welcome /} tag that generates the welcome message you’ve seen in the browser.

The #{extends /} tags tells Play that this template inherits another template called main.html. Template inheritance is a powerful concept that allows to create complex web pages by reusing common parts.

Open the helloworld/app/views/main.html template:

<!DOCTYPE html>
<html>
    <head>
        <title>#{get 'title' /}</title>		
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <link rel="stylesheet" type="text/css" media="screen" 
            href="@{'/public/stylesheets/main.css'}" />
        <link rel="shortcut icon" type="image/png" 
            href="@{'/public/images/favicon.png'}" />
    </head>
    <body>
        #{doLayout /} 
    </body>
</html>

Can you see the #{doLayout /} tag? This is where the content of Application/index.html will be inserted.

Creating the form

We will start the ‘Hello World’ application with a form where you can enter your name.

Edit the helloworld/app/views/Application/index.html template:

#{extends 'main.html' /}
#{set title:'Home' /}
 
<form action="@{Application.sayHello()}" method="GET">
    <input type="text" name="myName" /> 
    <input type="submit" value="Say hello!" />
</form> 

Note that we use GET as form submission method, because the form submission does not have any side effect and is idempotent.

We use the @{…} notation to ask Play to automatically generate the URL able to invoke the Application.sayHello action. Now, refresh the home page in the browser.

Oops, you get an error. This is because you reference the non-existent action Application.sayHello. Let’s create it in the helloworld/app/controllers.scala file:

package controllers
 
import play.mvc._
 
object Application extends Controller {
 
    def index = Template
    
    def sayHello(myName:String) = Template('myName -> myName)
 
}

We have declared the myName parameter in the action method signature, so it will automatically be filled with the value of the HTTP myName parameter, coming from the form submission.

This new action also returns a Template value, but this time it specify a value to be included in the template. You can specify as many values you want to send to the template, and for each you need to specify the template variable name. So each value is specified as a (Symbol, Any) tuple, where the first Symbol value will be used a template variable name.

Now, if you try to enter your name and submit the form, you will get another error:

The error is pretty clear. Play tries to render the default template for this action method, but it doesn’t exist. Let’s create it in the file helloworld/app/views/Application/sayHello.html:

#{extends 'main.html' /}
#{set title:'Home' /}
 
<h1>Hello ${myName ?: 'guest'}!</h1>
 
<a href="@{Application.index()}">Back to form</a>

You can refresh the page.

Look how we have used the ?: operator in the template. It switches to a default value if the myName variable is not filled. So if you try to submit the form without entering any name, it will display ‘Hello guest’.

Providing a better URL

Well, if you look at the submission URL, you will see something like:

http://localhost:9000/application/sayhello?myName=guillaume

It is not very pretty. This is because Play used the default ‘catch all’ route.

*       /{controller}/{action}                  {controller}.{action}

We can have a better URL by specifying a custom path for the Application.sayHello action. Edit the helloworld/conf/routes file and add this route after the first one:

GET     /hello                                  Application.sayHello

Now go back to the form and submit it again to check that it uses the new URL pattern.

Customizing the layout

As both templates that the application use so far inherit from the same main.html template, you can easily add a custom layout. Edit the helloworld/app/views/main.html file:

...
<body>
    The Hello world app.
    <hr/>
    
    #{doLayout /}
</body>
...

Now we have a common header for both pages.

Adding validation

We will add a little validation to the form, to make the name field required. We can use the Play validation framework to do that.

Edit the helloworld/app/controllers.scala file, and the sayHello action:

...
def sayHello(@Required myName:String) = {
    if(validation.hasErrors) {
        flash += "error" -> "Oops, please enter your name!"
        Action(index)
    } else {
        Template("myName" -> myName)
    }
}
...

And don’t forget to import the play.data.validation.Annotations._ values to get the @Required annotation.

Now our sayHello can return 2 distincts values. A Template value in the case of there is a name provided by the form, or an Action value if there is an error. An Action value is used to redirect to another existing action method. Here we specify that we want to redirect to the index action.

Play will automatically check that the myName field is filled or will add an error object to the errors scope. Then if there is any error, we add a message to the flash scope and redirect to the index action.

The flash scope allows to keep messages during action redirection.

Now you just have to display the error message if any. Edit the helloworld/app/views/Application/index.html:

#{extends 'main.html' /}
#{set title:'Home' /}
 
#{if flash.error}
    <p style="color:#c00">
        ${flash.error}
    </p>
#{/if}
 
<form action="@{Application.sayHello()}" method="GET">
    <input type="text" name="myName" /> 
    <input type="submit" value="Say hello!" />
</form>

And check that it works:

Writing an automated test suite

We will finish by writing a test for the application. As there is no Java logic to test, we need to test the Web application itself. So we will write a Selenium test.

First, you need to run your application in test mode. Stop the application and restart it with the test command:

$ play test

The play test command is almost the same as play run, except that it loads a test runner module that allows to run test suite directly from a browser.

Open a browser to the http://localhost:9000/@tests URL to see the test runner. Try to select all the default tests and run them; all should be green… But these default tests don’t really test anything.

A selenium test suite is typically written as an HTML file. The HTML syntax required by selenium is a little tedious (formatted using an HTML table element) to write. The good news is that Play will help you to generate it using the Play template engine and a set of tags that support a simplified syntax for selenium scenarios.

The default test suite of a newly created Play application already contains a selenium test. Open the helloworld/test/Application.test.html file:

*{ You can use plain selenium command using the selenium tag }*
 
#{selenium}
    // Open the home page, and check that no error occurred
    open('/')
    assertNotTitle('Application error')
#{/selenium}

This test should run without any problem with the application for now. It just open the home page and check that the page content does not contain the ‘Application error’ text.

Let’s write a test for our application. Edit the test content:

#{selenium}
    // Open the home page, and check that no error occurred
    open('/')
    assertNotTitle('Application error')
    
    // Check that it is the form
    assertTextPresent('The Hello world app.')
    
    // Submit the form
    clickAndWait('css=input[type=submit]')
    
    // Check the error
    assertTextPresent('Oops, please enter your name!')
    
    // Type the name and submit
    type('css=input[type=text]', 'bob')
    clickAndWait('css=input[type=submit]')
    
    // Check the result
    assertTextPresent('Hello bob!')
    assertTextPresent('The Hello world app.')
    
    // Check the back link
    clickAndWait('link=Back to form')
    
    // Home page?
    assertTextNotPresent('Hello bob!')
#{/selenium}

We have fully tested the application. Just select this test in the test runner and click ‘Start’. Everything should be green!

Learn more

This was a very simple tutorial about a very dumb application.

If you want to learn more about Scala, you can use our interactive Play with Scala application.