Sunday, January 17, 2010

Shopping Cart Web Application - Play Framework

 Shopping Cart Application – Play Framework

I recently spent time stepping through the yabe (yet another blog engine) tutorial for the Play Framework.

"The Play framework makes it easier to build Web applications with Java"


What is Play?
Play is a full stack open source web framework for Java that focuses on developer productivity.

"The Play framework compiles your Java sources directly and hot-reloads them into the JVM without the need to restart the server. You can then edit, reload and see your modifications immediately"


Why Play?
There are a number of reasons why the Play framework is cool and I highly recommend you take a look at it: 5 cool things you can do with Play. The one that caught my eye was that the Play framework eliminates the time spent in compiling Java source files, packaging the code into an archive and then deploying that archive to a server. Already I am hooked. This is great! For something similar to this for JEE development take a look at JRebel. Depending on which license you get you may have to pay a fee.

Shopping Cart
In an effort to learn more I decided to build my own Play web application based on the very basic Shopping Cart application I have used in the past.

Here are a few comparisons between the original Shopping Cart web application and this one written for the Play framework:

1. JPA
The Play framework supports JPA so there really isn’t any difference with the entity classes in the domain model.

2. Spring
In the original application I was mainly using Spring for dependency injection. I didn’t need to use Spring in my application but the Play framework supports Spring as well as dependency injection (in version 1.0.1).

3. JSF Components vs HTML
The original application used JSF to build the GUI interface whereas Play uses plain HTML. There are many different JSF components to choose from and you can use additional JSF component libraries besides the standard components that come with JSF such as RichFaces / ICEfaces and many more. With very little effort you can include a JSF component within your page and bind it to a Java class. It is all very cool for a Java developer who wants a nice looking website without hiring a web developer to do it for them. However there are a few drawbacks to this approach, some of the main ones being:

  • The JSF components render down to HTML and JavaScript. You have no real control over how the rendering takes place. You could be rendering a HTML table whereas you would prefer to use a DIV.
  • Some JSF components reference custom JavaScript files. These JavaScript files could be quite large and you may not even be referencing them but because they form part of the JSF library you automatically reference it. 
  • Not that easy to change the behaviour of the JSF components to do something that it was never designed to do.

The Play framework uses standard HTML as well as its own template engine. The drawbacks for a Java developer is there are no drag and dropping of components into a JSP page. However if you’re a web developer you would most likely prefer to work with plain HTML files. You have more control over your page and there are 100’s of CSS / JavaScript libraries you can pull into your project without much hassle.

4. AJAX
The original project used a very cool JSF library called RichFaces. RichFaces has many ‘rich’ components to choose from with built in Ajax support.

I used JQuery to do all the Ajax requests and responses in the Play framework. JQuery is a very powerful JavaScript library. However you are not forced to use it, you could just as easily do it with your own JavaScript or another library that offers similar functionality.

5. JUnit
Both the original application and Play use JUnit as the test framework. The Play framework has a couple of nice utilities you can use and comes with a test suite out of the box to run all your tests from.

That’s all I wanted to say on that, the Play documentation is quite good, for a taste of what you can do I highly recommend the yabe tutorial and they have an active forum for any questions.

6 comments:

BANSE Désiré said...

Hello,

I am a student, and I like working in web application.
I'am willing to profile a web application built with play, but in netabeans 6.8, I have to tell to the profiler the server the application is using. So far, i don't know how to do. May I ask you, how I can get my web application easily profiled ? Thank you in advance

rmahony said...

Hello BANSE,

I have never done this myself until today. At first I tried to use the -agentpath option when running the play application however I got a couple of exceptions that I won't go into here. I noticed in NetBeans you can connect to a running process. That approach seemed to work. To do this:

1. Start your play application (ex: play run yabe).
2. Open NetBeans
3. Profile -> Advanced Commands -> Run Profiler Calibration
4. Profile -> Attach Profiler -> click the change link in Attach mode: Local ...
a. Target type: J2EE Web/App Server
b. J2EE Web/App Server type: Generic Web/App Server
c. Attach method: Local
d. Attach invocation: Dynamic
5. Click the Finish button
6. Attach to: Select the Play process that is running

Note I am running the latest update of Java: JDK 6 update 20

I hope this helps

BANSE Désiré said...

Thanks a lot Ross. It works perfectly . I had to launch the application before attaching the profile. Connecting to a running process is really helpfull . I'm now going to run some load generator scripts with Jmeter from my previous "classical" web application. I hope, that will work.

Jitesh said...

Hi, I'm Jitesh, and I work for Packt. I am looking for reviewers for our recently published book, Play Framework Cookbook written by Alexander Reelsen.

I found that your blog is informative, and has articles on Play! If you are interested in reviewing the book, then I'd be glad to send you an e-copy of it.

More information about the book: http://www.packtpub.com/play-framework-cookbook/book

For more details, contact me on: jiteshg@packtpub.com

rmahony said...

Hi Jitesh, I would like to very much review the book. I sent you an email

Zinavo .Co said...

Getting the prospects to arrive at their site is only the first of several hurdles in guiding customers from an entry point on website to the payment processing pages.
Website Designing Companies | Bangalore Web Development Company