Tuesday, April 03, 2007

Prototype, Sitemesh and Struts

The Java environment for my current project is stuck at Java 1.4, but that doesn't mean I have to build my webapps like it's 2002 (that's when 1.4 was released). As a matter of fact I decided to take some lessons from Ruby on Rails.

First of all I decided to ditch Tiles in favor of Sitemesh. Tiles gets the job done but at the expense of a little too much abstraction for my taste. Sitemesh uses the decorator pattern and just simply feels a whole lot cleaner. While not as simple as dropping an RHTML file into a "layouts" directory, it's certainly a whole lot easier than the XML config needed for Tiles.

Secondly, I use DispatchAction to get multiple "action" methods in one Struts Action subclass. It's nothing I haven't done before but keeping the number of classes down is a good thing.

Finally I decided I liked the way RoR uses Prototype and Scriptaculous to provide a little more responsiveness to my webapps. So I decided to jump into Javascript and use Prototype in my Struts webapps. I had to configure Sitemesh to leave my AJAX responses alone and I needed to read a bit of the Prototype docs but it was pretty straightforward. I used the Rails partial convention to let my Java app generate HTML fragments through a Struts action and a JSP and then let my Javascript function replace a piece of the DOM with the result of the AJAX call.

When given lemons, make lemonade. When given ancient Java technology, spice it up with the techniques you learn by looking at alternative implementations.

1 comment:

Radha Krishna said...

came across your post when searching for struts + prototype

Any hints on where I should start looking for or may be how you got started on it?

I have been using struts for a while and am looking forward to include prototype!