This is a very superficial guide that will only introduce you to the basic functionality of the Play framework using the example of creating the 'Hello World' application.
You can get acquainted with the Play framework in the article
“Excellent Java MVC Framework - Play Framework” . And this translation describes the process of creating a web application.
Requirements
Make sure that you have installed Java machine not lower than version 5.
')
Since we will actively use the command line, it is best to use Unix-like OS. If you are working under Windows, the Play framework will also work fine, you will need to enter several commands on the command line.
You will of course need a text editor. If you are used to using fully featured IDEs such as Eclipse or Netbeans, you can use them. However, with Play, you can work in a simple text editor, such as Textmate, Emacs, or VI. This is because Play manages compilations and deployments on its own. We will soon see this ...
Installing the Play framework
It is very simple. You only need to download the latest version and unpack to any convenient place. (
Download )
If you are using Windows, then it is a good idea to use the path to files without a space character, for example, c: \ Play is better than c: \ Documents And Settings \ user \ play.
To work more efficiently, you need to add the Play directory to the PATH environment variable. This will allow you to use the play command in any directory. To check that everything works, it is enough to open the command line and enter play, it must show you the help on the main commands.
Creating a project
Now that the Play framework is installed, it's time to create an application. Creating it is fairly easy and completely controlled from the command line. Play allows you to create standard application templates.
Open a console and enter:
~$ play new helloworld
You will be asked to enter the full name of the project. Enter "Hello world".
The play new command will create the helloworld / directory and fill it with a number of files and directories, the most important of which are:
app / contains the core of the application: models, controllers and views. It may also contain other packages of Java classes. This is the directory where the application sources are located.
conf / contains the application configuration files, the main configuration file is application.conf, the routes define the available addresses and the messages file that is used to internationalize the application.
lib / contains additional Java libraries.
public / contains publicly available files that include javascript, style files and images.
test / contains all application test cases. Tests can be written for both JUnit and Selenium.
Since Play uses UTF-8 as the only encoding, it is very important that all text files located in these directories be in UTF-8. Make sure the text editor is set accordingly.
You may be wondering where the .class files are stored. The answer is there are none: Play does not use .class files, it reads directly the source files. The framework uses the Eclipse compiler to compile on the fly.
This gives two very important things in the development process. First, Play will detect changes made to any Java source file and automatically recompile them at run time. Second, when an error occurs, Play will generate informative error reports, showing you the exact source code of the error.
Application launch
Now We can test the application you just created. Just go back to the command line, go to the helloworld / directory and run the play run command. Play will download the application and launch the web server on port 9000.
You can see the result of the application by opening the address
http: // localhost: 9000 in the browser. The new application has a standard welcome page. Application successfully created.

Let's see how the app displays this page.
The entry point to the application is defined in the conf / routes file. It identifies all available addresses in the application. If you open this file, you will see:
GET / Application.index
This line tells Play that when the web server receives a GET request /, it should call the Application.index method. In this case, Application.index is a shortcut to controllers.Application.index, since the controllers package is implicit.
When creating Java applications, you usually use a single entry point, which is defined in this way:
public static void main(String[] args) { ... }
The Play application has several entry points, one for each URL. We call these methods action methods. Action is defined in special classes that we call controllers.
Let's see what the controller controllers.Application is. Open the source helloworld / app / controllers / Application.java:
package controllers; import play.mvc.*; public class Application extends Controller { public static void index() { render(); } }
You may notice that controller classes inherit play.mvc.Controller. This class provides useful methods for controllers, such as the render () method, which is used in the index () function.
The index () function is defined as public static void. So defined methods of action. Action methods are static since a class controller is never created. They are marked as public to allow the framework to call them in response to a URL. They always return void.
Calling the default action is simple: just call the render () method, which will let Play know that you need to show the template. Using templates is the most common (but not the only) way to get an HTTP response.
Templates are simple text files that are located in the / app / views directory. Since we did not specify a template, the default for this action will be used: Application / index.html
To see what the template looks like, you need to open the file helloworld / app / views / Application / index.html. He contains:
#{extends 'main.html' /} #{set title:'Home' /} #{welcome /}
The design pattern seems pretty simple. In fact, all you see are Play tags. Play framework tags are very similar to JSP taglib. Tag # {welcome /} - generates a greeting that you saw in the browser.
The # {extends /} tag indicates Play that this template inherits the main.html template. Template inheritance is a powerful concept that allows you 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>
Tag # {doLayout /} - the place where the contents of the Application / index.html file will be inserted.
The end of the translation will be in the second part, in which the work with the forms, the design change and the automatic test will be created.(Part 2)