Play framework

Creating an Admin Application with Play

I’ve been looking quite a lot at the Play framework recently and I really like everything about it. It is easy to use, the development cycle is awesome and the modules make it really easy to bring new new functionality into the application. But so far I’ve only used it for pet projects. So I was curious to find out how it plays in the enterprise

After a quick poke-around I found a suitable case for trying out the CRUD functionality in Play. We keep the prices for the products we sell on the web site in a database. Oddly enough, we do not have an administration interface for the 4 tables. This means that new products are added through sql scripts by resources in the IT-department. The process works. But if we had an administration interface then the people responsible for the products could maintain the prices as well.

(Ok, I know that this is an extremely small application and that enterprise applications are supposed to be huge and complex. But even in enterprises there are some fairly straight forward applications to be built, we just need to pick the right tools for the job, in this case I think Play could be the right tool)

The solution consists of 4 tables with a kind of strange structure, well it is certainly not in third normal form. Let’s see how easy it is to create a CRUD administration interface using Play.

1. Generate a new play project

From the command line I ran the create new play app command: play new pricedb

2. Create the Model objects

Next, create model objects for each of the four tables and add public properties for each of the fields in the table. Yes, you read correctly, public. There is a good reason for this which is explained in the Play documentation

3. Create CRUD controllers

First enable CRUD support in your application by adding the following in the /conf/application.conf file

# The crud module

For each Model object create a CRUD controller class. Don’t worry, it is extremely easy. For our Product model object this class will look like this:

package controllers;
public class Product extends CRUD {

Finally add the default CRUD module to the routes file

# Import CRUD routes
*      /admin              module:crud

4. Running the application in-memory

That’s it, we’re ready for a test. It’s easy to run the application in-memory HSQL, just make sure you have the following line in your /conf/application.conf file


The admin application can be accessed on http://localhost:9000/admin/

5. Switching to an oracle database

Running an administration interface in memory is kind of useless, so it’s time to connect to a real database. Luckily this is almost as easy as running the database in memory. Hugo Monteiro has written a nice little post about how to set up a db connection to oracle.

When you start up the application again you can see in the console that the application connects to the database. The application is still accessible on /admin/, but when trying to access one the objects we get an exception:

Internal Server Error (500) for request GET /admin/pricetypes

Template execution error (In {module:crud}/app/views/tags/crud/table.html around line 76)
Execution error occured in template {module:crud}/app/views/tags/crud/table.html. Exception raised was NullPointerException : Cannot get property 'id' on null object.

play.exceptions.TemplateExecutionException: Cannot get property 'id' on null object
at play.templates.Template.throwException(
at play.templates.Template.render(
at play.templates.Template$ExecutableTemplate.invokeTag(
at {module:crud}/app/views/CRUD/list.html.(line:12)
at play.templates.Template.render(
at play.mvc.results.RenderTemplate.(
at play.mvc.Controller.renderTemplate(
at play.mvc.Controller.render(
at controllers.CRUD.list(
at play.utils.Java.invokeStatic(
at play.mvc.ActionInvoker.invoke(
at Invocation.HTTP Request(Play!)
Caused by: java.lang.NullPointerException: Cannot get property 'id' on null object
at {module:crud}/app/views/tags/crud/table.html.(line:76)
at {module:crud}/app/views/tags/crud/table.html.(line:36)
at play.templates.Template.render(
... 10 more

It turns out that the tables in the database does not have an id. Having one is considered good practice, so I decided to add the Id field to each of the tables in the database (you need to restart the server to see the changes).

Restart, and: Voila. I can now see the rows in the tables when clikcing on it on the admin frontpage. I can add, delete and update items as well as listing them. Not bad after only an hour or two. Well, the look and feel is not great. Well, that can be fixed later.

But when taking a closer look at the rows in the database, I notice that they don’t really look like the original tables, and since I’m adding this to something existing I want to make sure that the tables look exactly like the old ones. Or do I?

If it’s hard to do, it’s probably not right

So I did a couple of searches on the internet to try and find a solution to my problem. I was starting to suspect that this would not be an easy task. My suspicion was further confirmed when I found a question about CRUD on existing tables on the Play Framework forum.
When you encounter problems it is often a smell. Our smell in this case is that data is duplicated in several tables, it’s simply a poor database design. When identifying prolems like these, I believe we should take the time to clean it up. In this case it means that:

  • data needs to be migrated from the original tables to the new one
  • small modification to sql responsible for retrieving the data so that it may be displayed on the web

Two small exceptable tasks if you ask me. If you want to improve you need to change stuff once in a while.

6. Improving the look and feel

With a few changes to configuration files, generation of templates to override the defaults provided by the framework, you can create a classy look and feel to your administration interface. The CRUD section of the extremely well-written play documentation is a good source of information on this subject.

Does Play cut it in the Enterprise?

Yes, I think so. Part of our job is finding the right tool for the right problem and Play is highly suitable for solving some of the problems in the enterprise. Administration applications is one of many areas this excellent framework can make life easier for enterprise developers.


One thought on “Creating an Admin Application with Play

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s