I’m a swiss Master student in Computer Science. I’m very interested in C++, open source projects, Linux, Intel Assembly and Agile. I'm currently working on Eddi, a new programming language that I created to improve my skills in C++. I've also worked a lot on Java technologies (Sprint, Osgi, Play!, ...), but I'm not currently working with Java anymore. Baptiste is a DZone MVB and is not an employee of DZone and has posted 51 posts at DZone. You can read more from them at their website. View Full User Profile

Getting Started With the Play Framework

07.06.2010
| 8868 views |
  • submit to reddit

It’s time for me to test the Play Framework and I’ll try to make some posts during my tests about this web framework. The Play Framework is a framework for creating web applications in Java. The main goals of this framework are (according to the official site) :

  • Hot reload : You can edit your Java files and HTML files and you just have to refresh the browser to see the results
  • Stateless model : Play is ready for REST, it can be scaled running multiple instances of the same applications on several servers
  • Efficient Template System : The template system is really easy to use, based on Groovy.
  • Resolve errors quickly : When an error occurs, Play displays the source code directly in the browser along with the location of the error
  • Integration : Play provides integration for Hibernate, OpenID, MemCached and others popular frameworks
  • Pure Java : You make only Java and HTML, there are no other things to learn and you get easy integration in IDE.
  • Fast : The startup of application is really fast, as is the rendering of the pages.

In this post, we’ll see how to install the Play Framework and how to write our first Hello World.

Install Play Framework

Before installing Play Framework, you need Java 5.0 or later. Then, you can download a release of Play : http://download.playframework.org/releases/. The latest when I wrote this post is the 1.0.3 version. So let’s download this version. After that, just unzip the given archive to where you want to install the framework. After that, you just have to add the play command to the path.

In Ubuntu 10.04, I had to do the following :

PLAY_HOME=/usr/share/apps/play/
PATH=$PATH:$PLAY_HOME
chmod +x PLAY_HOME/play

Depending on your configuration, you perhaps have to use sudo for the chmod command. For other systems (Windows and Mac), you also have to add play (play.bat for Windows) to the path using the system configuration.

And normally, after that, you can execute the play command :

wichtounet@Linux-Desktop:~$ play
~ _ _
~ _ __ | | __ _ _ _| |
~ | '_ \| |/ _' | || |_|
~ | __/|_|\____|\__ (_)
~ |_| |__/
~
~ play! 1.0.3, http://www.playframework.org
~
~ Usage: play cmd [app_path] [--options]
~
~ with, new Create a new application
~ run Run the application in the current shell
~ help Show play help
~
wichtounet@Linux-Desktop:~$

Creating a new application

To create a new application, you just have to use the command

play new app_name

You must be in a folder where a folder with the name app_name can be created. For example, let’s create a new application “hello” :

wichtounet@Linux-Desktop:~/dev/play$ play new hello
~ _ _
~ _ __ | | __ _ _ _| |
~ | '_ \| |/ _' | || |_|
~ | __/|_|\____|\__ (_)
~ |_| |__/
~
~ play! 1.0.3, http://www.playframework.org
~
~ The new application will be created in /home/wichtounet/dev/play/hello
~ What is the application name? Hello World
~
~ OK, the application is created.
~ Start it with : play run hello
~ Have fun!
~
wichtounet@Linux-Desktop:~/dev/play$

During the creation, Play will ask you for the name of the application. Here I put “Hello World” as the name of the application. If you go into the new created application folder, you will see the given folders :

  • app : The application itself, Java classes and HTML files
  • conf : The configuration of the application
  • lib : Contains the Java libraries that the application needs
  • public : Contains the public files, images, CSS, JS
  • test : Contains the tests (JUnit or Selenium) file

You can launch your application, with the command :

wichtounet@Linux-Desktop:~/dev/play$ play run hello
~ _ _
~ _ __ | | __ _ _ _| |
~ | '_ \| |/ _' | || |_|
~ | __/|_|\____|\__ (_)
~ |_| |__/
~
~ play! 1.0.3, http://www.playframework.org
~
~ Ctrl+C to stop
~
Listening for transport dt_socket at address: 8000
17:49:56,395 INFO ~ Starting /home/wichtounet/dev/play/hello
17:49:56,889 WARN ~ You're running Play! in DEV mode
17:49:56,958 INFO ~ Listening for HTTP on port 9000 (Waiting a first request to start) ...
17:50:01,670 INFO ~ Application 'Hello World' is now started !

And you can browse your application at the URL http://localhost:9000/. You will normally see a sample page :

Play Framework sample page

Play Framework sample page

How does it works ?

So now, let’s study why this work. If you open the file conf/routes, you will see that line :

GET / Application.index

That indicates that the index of the project will be serviced from the index method of the Application class. And if you open the app/controllers/Application.java file, you will see :

package controllers;

import play.mvc.*;

public class Application extends Controller {
public static void index() {
render();
}
}

So at this time, the index() only renders the default template. This template is app/views/Application/index.html (like the name of the method and the class) :

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

#{welcome /}

We can see that this template extends the main.html template. It sets the title to home and display the welcome prompt. These tags are tags from the Play Framework. Lets change this template to :

#{extends 'main.html' /}
#{set title:'Hello World' /}

<h3>Hello the world !</h3>

And refresh the application page. You will see now only the message “Hello the world”. If you want to see something when the controllers is called, just add a System.out.println in the Application.index :

public static void index() {
System.out.println("render()");
render();
}

And you will see logs in the console when you refresh the pages :

17:50:01,670 INFO  ~ Application 'Hello World' is now started !
render()
render()
render()
render()
render()

Add a simple new page

To do a little more in this post, let’s add a new page : http://localhost:9000/bye. Let’s start adding a new route in conf/routes :

GET     /bye                                     Application.bye

So we have to add a method bye in the Application class :

package controllers;

import play.mvc.*;

public class Application extends Controller {
public static void index() {
render();
}

public static void bye() {
render();
}
}

And then, we just need to add a new template bye.html in app/views/Application/ folder :

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

<h3>Bye bye !</h3>

And now, if you go to http://localhost:9000/bye you will see our new page :

A Bye page in Play Framework

A Bye page in Play Framework

Conclusion

Here we are ! With this framework, it’s extremely easy to create web applications. And we’ve only seen a very small part of the facilities that this framework offers to create web applications. I’ll try to write others posts on the subject.

From http://www.baptiste-wicht.com/2010/07/getting-started-play-framework/

Published at DZone with permission of Baptiste Wicht, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

Comments

Martin Grigorov replied on Tue, 2010/07/06 - 3:22am

In the beginning of the article you cite: Hot reload : You can edit your Java files and HTML files and you just have to refresh the browser to see the results
Do you know how this actually works ? Seeing usage of static methods makes me nervous.

Baptiste Wicht replied on Tue, 2010/07/06 - 7:26am

From what I know, Play doesn't use the compiled classes. It directly reads the Java files and use the Eclipse compiler to compile them when it needs them. After that, it detects the modification on the files and recompile the modified files automatically. So I don't think there will be any problem with static methods. But perhaps, I didn't understand what your thought.

Anthony Goubard replied on Tue, 2010/07/06 - 10:12am in response to: Martin Grigorov

If it's just a change in a method, the standard Java hotfix is used via a Java agent.

If you've modified the signature, added or remoevd methods or class, the whole application is redeployed. But as it's stateless (actually the state is on the client), you don't need to login again or to go to the page you were before redeploying.

Martin Grigorov replied on Wed, 2010/07/07 - 3:06am in response to: Anthony Goubard

I just don't get why the methods need to be static ?
That will involve all other mehods in this class to be static too ...
How do you extend/reuse the controller? Since the method is static there is no way to override it in a sub-class.
And we all learn our lesson with statics and unit testing + mocking .

And redeploying is step back I think. That's the reason for products like JRebel - to prevent redeployments. Because if you work with HelloWorld app then it is ok, but if you have more serious application then it is not very productive to wait 2-3 minutes for redeploy for single change - added/removed method, added import, ...

Kode Ninja replied on Wed, 2010/07/07 - 5:39am

Is it possible to use Groovy, instead of Java, with Play?

Senthil Balakrishnan replied on Wed, 2010/07/07 - 8:41am

Interesting name :), will give a try sometime.

 

Mike P(Okidoky) replied on Wed, 2010/07/07 - 9:05am in response to: Anthony Goubard

It should be possible to integrate JRebel which is a little better than hot swap...

Baptiste Wicht replied on Fri, 2010/07/09 - 6:02am in response to: Martin Grigorov

Guillaume Bort, the lead developer of Play has answered this question : Because the controller part is not object oriented. It should be as light as possible and deleguate all the work the application model. The controller layer is the HTTP service of your application.

Baptiste Wicht replied on Fri, 2010/07/09 - 6:07am in response to: Kode Ninja

No, it's not possible.

Sebastián Open replied on Tue, 2010/07/20 - 6:11am in response to: Martin Grigorov

I'm not a play guru at all, but I think that the whole redeploying stuff works only on dev mode. on production mode everything is compiled once and for all...

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.