Friday, July 24, 2009

Java Templating APIs

Many web view technologies are very difficult to unit test. This includes JSP/JSTL, PHP views, Rails views, and probably ASP.NET views. The problem is the lack of the ability to set an arbitrary model to a view and doing a string assertion on the view. (Correct me of I'm wrong.)

I did a bit of research on templating engines for Java. Here they are:

Freemarker - Not specific to the web, unit testable without servlet container, replacement of JSP. Last update in December 2008.

Velocity - Greater community than Freemarker (according to Freemarker themselves). Also not web specific and a replacement for JSP. Last update in May 2009.

String Template
- Very minimalistic approach to templating with ports to Python and C#. Also not specific to the web. Last update June 2008.

All of these technologies basically solve the problem of making the view accessible without a servlet container. This makes it easier to pass the responsibility of HTML/CSS design and JavaScript coding off to other developers. Spring supports Velocity and Freemarker though it shouldn't be too difficult to write an adapter for String Template. Right now, I'm leaning towards Velocity because of the better community support.

In addition to templating engines, I also looked at page composition frameworks:

Tiles - Originally part of Struts. Employs the composite pattern where each page explicitly define header, footers, and etc. to include. Support for Freemarker and Velocity templates. Last release in February 2009.

SiteMesh - Alternative to Tiles. Employs the decorator pattern where pages are not aware of the header and footer. Can work with other web technologies (CGI, PHP, and etc.) -- this is COOL. Last update in March 2009.

I am really excited about using SiteMesh. Here's a SiteMesh tutorial.

1 comment:

  1. Thanks for the post, I am techno savvy. I believe you hit the nail right on the head. I am highly impressed with your blog. It is very nicely explained. Your article adds best knowledge to our Java Online Training from India. or learn thru Java EE Online Training Students.