Book Image

Instant Play Framework Starter

By : Daniel Dietrich
Book Image

Instant Play Framework Starter

By: Daniel Dietrich

Overview of this book

Play is a full-featured Java and Scala web framework for building modern, high-performance web applications. It is characterized by its simplicity and scalability. With its lightweight, stateless, and web-friendly architecture, Play focuses on developer experience to make web application development fun.Instant Play Framework Starter is the ideal companion to start developing web applications with Play. The building blocks of a typical web application are carefully designed following an on-going example.Instant Play Framework Starter starts with a quick setup and running a first sample. Then, the anatomy of a typical Play application is outlined. More features are added step by step to an example application. The result is the prototype of a highly scalable web application.The example is implemented in Java and in Scala. It consists of building blocks you will find in every Play application. In particular, you will learn how views are rendered with the template engine, how HTTP routes are used to define the navigation rules, and how to separate the application logic of controllers from the business logic of the model. This separation is the result of a careful application design, which makes it easy to add features like data binding and validation. Finally you will see how easy it is to adapt different database access libraries. Instant Play Framework Starter will help you to get started with Play and develop your first application. Packed with examples, it is easy to follow the design of a real-world application. You are able to compare the difference between a Java- and a Scala-based Play application and to decide which language fits your needs best. All topics covered in the book are described with the aim to serve as a reference for future web application development with Play.
Table of Contents (7 chapters)

Installation


In four easy steps, we can install Play and get it set up on our system.

Step 1 – What do I need?

Before you install Play, you need to check if JDK 6 or later is installed, which can be downloaded here: http://www.oracle.com/technetwork/java/javase/downloads

Tip

The JDK should be pre-installed on Mac OS X and Linux. The OpenJDK, a viable open-source alternative to the Oracle JDK, is already a part of many Linux distributions. Windows users should download and install the latest JDK as mentioned previously.

This is the only requirement. Play is bundled with its own web server and build environment, so we don't have to install additional software.

As mentioned previously, Play provides more than just an API for programming web applications—Play is a full-stack web framework that also takes care of building and running your application. You can use your preferred editor or IDE to develop a Play application. Modifications to your source code are automatically detected and the running application is refreshed by Play.

Step 2 – Downloading Play

The easiest way to download Play is as a compressed package from http://www.playframework.org/download.

Tip

For Mac users with Homebrew installed, it's even easier to install Play. The command brew install play will install the latest version of Play and set the PATH environment variable accordingly. For managing multiple versions of Play, it is definitely worth taking a look at https://github.com/kaiinkinen/pvm.

We suggest that you download the most current stable build. After downloading, the archive has to be unpacked to a place where you have read and write permissions. This is necessary because Play caches all library dependencies in a file system repository. After unpacking the archive, you will be left with a directory called play-2.1 or similar, depending on the downloaded version. The extracted directory contains a number of files and folders.

/path/to/play
   | CONTRIBUTING.md    instructions for committing code
   | README.md          basic instructions on using Play
   | play               Mac/Linux start script
   | play.bat           Windows start script
   |-documentation      manual and API specification
   |-framework          Play project files and sources
   |-repository         downloaded dependencies
   |-samples            sample application for Java and Scala

Here you find the Play start scripts play for Mac/Linux and play.bat for Windows. The documentation folder contains the user manual and the API specification, which will accompany you when developing an application. In the samples folder, you will find the source code of the examples shipped with Play. They are a great learning resource when you have understood the basics of Play and want to go further, so take a look at them. Both the documentation and the samples are available for Java and Scala developers.

The remaining folders are of less interest to us in this book; framework contains the Play project files and sources and repository contains additional downloaded libraries and dependencies.

Step 3 – Setting the PATH environment variable

Before creating our first Play application, we have to ensure that the Play installation directory is added to the system path. This makes it possible to start Play from the console without specifying the complete path of the start script.

Please open a text editor of your choice. Depending on the underlying OS, the PATH environment variable is set up as follows:

  • Mac OS: To set up the PATH environment variable, add the line export PATH=$PATH:/path/to/play to the file ~/.bash_profile, where /path/to/play points to the location where you have installed Play.

  • Linux: To set up the PATH environment variable, add the line export PATH=$PATH:/path/to/play to your shell configuration file, for example, add the line to ~/.bashrc if you use the bash shell. Again, /path/to/play points to the Play installation directory.

  • Windows: To permanently add Play to your environment settings, open a terminal window and execute the following command: setx PATH "%PATH%;c:\path\to\play" -m. Please change the path c:\path\to\play according to the path of your Play installation directory.

Step 4 – Testing the Play installation

Now we are ready to start Play. Test your installation by opening a new command-line window. Then type the play command. The Play script should produce output on the screen similar to this:

The output of the play script shows us the error message This is not a play application!. You will see that it is characteristic for Play to provide hints for fixing errors. In this case Play gives us two options:

  • Use play new to create a new Play application in the current directory

  • Go to an existing application and launch the development console using play

We will discuss both topics in the next section.

And that's it!

Congratulations, by this point, you should have a working installation of Play and are free to play around and discover more about it.