I am a programmer and architect (the kind that writes code) with a focus on testing and open source; I maintain the PHPUnit_Selenium project. I believe programming is one of the hardest and most beautiful jobs in the world. Giorgio is a DZone MVB and is not an employee of DZone and has posted 637 posts at DZone. You can read more from them at their website. View Full User Profile

Web applications with the Play framework

11.10.2011
| 9331 views |
  • submit to reddit
The Play framework defines itself as a clean alternative to bloated enterprise Java stacks. I like this definition and after having been advised to try it out, I set out some Pomodoros for coding a pair of Hello, World applications in Play.

What the docs say

The first impressions from the documentation is very good: Play is a full stack framework already integrate with Hibernate, memcached and other tools. This is not necessarily the best choice for the long term, but it's a bliss for a PHP programmer who wants to experience a new platform without debugging exceptions (as long as the integration works well.)

The framework is written in pure Java, so it can interface with any JAR. However it also supports Scala code natively, and that's why I chose it for my experiments. Developing Scala web applications from scratch requires to setup boring scripts for compiling sources and deploying the right JAR files. Again, the best solution is not necessarily to outsource everything to a framework, but in my case it's the path of least resistance.

Play is also a shared nothing system, like PHP applications. It scales automatically by multiplying the instances (if I understood correctly, without relying on a single application server.)

Hello world in Java

Download and unzip Play where you want. The play command line utility will be used to create new projects, and needs to be sylinked on the our PATH.

A command line utility is better than Eclipse automation in my opinion: it's always possible to reuse and integrate these utilities in scripts, while it is not usually so with the IDE's features. Moreover I would like to use a framework no matter which is my IDE: when I worked with SMILA I was forced to write code in Eclipse.

The framework manages the compilation and the deployment process itself. I couldn't say it better.

Creating an application and starting it is really easy, and produces no exceptions:

play new helloworld
play run helloworld

If you look at the helloworld/ folder, you'll see no .class files. The source code is compiled by Play after each modification, as long as it is running. Initially, an empty application will display an help page.

Basic features

Routes are easy to add, although advanced syntax requires a bit of studying. However, routes are really integrated and you will never write an URL yourself, but just leverage referr to urls as their corresponding action with *class.method*.

Controller classes are static, which is a bit weird but not very important since I never write much code in them. They accept HTTP parameters as method parameters (like Symfony2):

    public static void sayHello(@Required String myName) {
        // call this with url?myName=...

As you can see, Play provides annotations for input validation, but does not redirect away from the action by default: you have control on what happens after a failed validation.

The templates are rendered by passing them their data dependencies; Play features a simple language for templating, which supports inheritance to provide layouts.

The Hello world tutorial for Java applications contains some screenshots and all the code you will want to check out.

Writing Scala code

Scala support is provided by an additional module, which can be installed directly via the command line:

play install scala
play new helloworldscala --with scala
play run

The classes of this application will be written in Scala:

object Application extends Controller {
    import views.Application._

    def index = {
        html.index("Your Scala application is ready!")
    }

}

After adding a bit of logic in the controller, it looks like this:

def sayHello = {
    val myName = params.get("myName")
    if(myName == "") {
        flash += ("error" -> "Oops, please enter your name!")
        Action(index)
    } else {
        html.sayHello(params.get("myName"))
    }
}

The Scala module features a different template engine: template have types such as (String) => Html in the case of a single parameter. You won't extend a template to build a layout, but include it and passing it an Html argument:

@main(title) {
    Hello, world
}

This template will print Hello, world inside the layout, which is built in the main template. External templates are just other objects where you can pass in parameters like the title of the page.

The Scala hello world example provides the same application as the Java hello world, but implemented with Scala code. I walked through it and again I reached the end without exceptions, which is not easy for a PHP developer in the JVM world. It probably means the framework is mature enough for anyone to use, and the precision the documentation is written with is really beneficial for the naive developer. Even exceptions are well-displayed and focused:

Conclusions

Play will definitely be my choice for JVM-based web applications. It's easy to work with and promises not to be a drain on productivity. It is certainly the fastest way to get Scala code accessible via HTTP: you don't even need an IDE to get started.

Published at DZone with permission of Giorgio Sironi, 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.)

Comments

Sebastián Open replied on Thu, 2011/11/10 - 11:45am

"path of least resistance", that phrase describes exactly my experience with Play! framework Coming from a classic asp / php background, it allowed me lo learn java (not only the language, but the ecosystem) while learning play... I think it's a very good choice for java devs eager to try something more agile as well as for people coming from dynamically typed languages, like phyton, ruby or php saludos sas

Giorgio Sironi replied on Sun, 2011/11/13 - 7:59am in response to: Sebastián Open

For sure EJBs are not the path of least resistance. :)

Comment viewing options

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