Friday, October 26, 2007

Simple Nested Tab Menus for Java web applications

I had a bit of a eureka moment when I finally realised that navigational menus belong at a layer above the core functionality that a web application provides. Borrowing an AOP term, navigation, especially menu navigation, can often be a cross cutting concern.

In a Java web application it is possible to layer navigation on top of a web application by using the Sitemesh servlet filter. Menus that appear simple to the user can actually be quite complicated to implement. I was looking through the example menus layouts that Listamatic2 has to offer and a couple of the nested menus really took my fancy (i.e. Blue's clues and Horizontal in horizontal). Some further investigations lead me to a fantastic example as used on Yahoo! News and described on their design pattern site, they even provide example code.

I recognised that this type of hierarchical manipulation was prime XML territory and consequently created a working example. I am very pleased with the results; I think it is a most elegant solution! The image above is taken from my implementation based upon the Yahoo example code. The menu configuration is a simple XML file and is therefore easily changed. Changing the menu and moving menu options about is very easy to do. The Sitemesh filter performs an XSL transformation that displays the tab menu transitions (performance can be improved by caching the XML/XSL in application scope). In this example my core functionality is a skeleton Spring MVC application but it could easily be any suitable Java view technology (JSP, Struts, Freemarker, Velocity etc.). Download it at the link below:

Download nestedmenus.zip ~8kb

Again to keep this distribution to the bare bones, it requires you to install Maven 2 which will download the Java libraries that the application needs. Download my distribution, unzip and run:

mvn compile war:war

Monday, October 22, 2007

I relented to Spring MVC; easy install IE7 feed reader web application

I have been actively using the Spring Framework for almost 2 years exactly. I really love it and use in all my DAOs. That said, until recently I have resisted the temptation to use Spring MVC. I didn't actually like Spring MVC (I have tried to use it several times but found it hard to convert). I was used to the structure that comes with Struts 1.2.X, I was more than happy to use the Spring ActionSupport which gives my Struts actions access to the Spring WebApplicationContext. What I liked about Struts 1.2.X is that you can give your struts-config to another Struts developer and they can work out exactly what is happening without much effort, this is a strength. I thought Spring MVC was a little too much of a free for all. Spring Web Flow gives Spring more structure but I was familiar with Struts and was understandably resistant to change. I found the idea of a ModelAndView class, multiple controllers, and view resolvers a little strange (as Struts usually has one controller).

I have finally relented and started to write with Spring MVC. This came about as I was recently working with XFire to produce web services and gradually discovered that XFire's elegance owed much to the internal use of Spring MVC. I don't know what has changed but I am suddenly much more comfortable with Spring MVC (maybe because it is easier in Spring 2.0, maybe it is just my Spring experience has grown). What I really like about Spring MVC is unlike Struts it is not overkill to use Spring MVC for very small applications.

The following is actually my second Spring MVC application (incidentally, my initial effort was to use JA-SIG CAS for authenticating XFire web services using WS-Security). This is a much simpler application and for regular readers of my blog (as if!) this will be familiar territory. This distribution makes use of Maven 2 (my favourite toy after Spring), using Maven 2 my whole distribution (17kb) is probably smaller than this web page!

I have written a single Spring MVC controller which returns an XsltView. What is special about this is that I use it to invoke XSLT 2 (Saxon again!). This results in an easy to install distribution of my previous IE7 emulator work. Using XSLT 2 with Spring MVC is actually incredibly easy.

Download IE7Emulator.zip -~17kb

All you need is to install Maven 2, download my distribution and run:

mvn compile war:war

and you are good to go! Enjoy!