Thursday, June 11, 2009

A simple example with StringTemplate

I have always wanted to write this article but often find myself preoccupied with many other things. Please spend 20 minutes of your day to read this article. It is a food for thought in my opinion and the main audience are Java developers developing web applications using Eclipse together with Apache Tomcat.

I would like to introduce a very powerful and convenient tool to generate HTML pages using StringTemplate. This tutorial assumes that the readers are well versed with writing servlets, configuring web server and Java programming. The main intention and focus of this article is to expose ourselves to the power of this template engine using Apache Tomcat web server; not Jetty, not Resin, nor JBOSS. I hope you find this blog entry useful although I will not go in depth with the nitty gritty of the details.

A short history of StringTemplate: this tool is written by Terence Parr, my former Computer Science professor at University of San Francisco. It is a useful tool to generate HTML pages dynamically and enforces a distinct and clear separation between view and model in the MVC model.

Tools that I use: Eclipse 3.4.0, Apache Tomcat 6.0 and of course the two important jar files that you need to download from the StrngTemplate official website. They are antlr-2.7.7.jar and stringtemplate.jar. Unpack the compressed file and copy the two jar files into WebContent/WEB-INF/lib folder of your project in the Eclipse IDE.

DISCLAIMER: the codes shown below are adapted from Terence Parr's sample implementation at his website.

Now let us get into the meaty part of this article by introducing the servlet dispatcher. Imagine the job of a postman sending letters to different destinations. However in our context, my servlet dispatcher is actually dispatching the users' requests to the appropriate servlets. I will name this class FrontendServletDispatcher and a sample of implementation is as below:As you may notice in the implementation of FrontendServletDispatcher, there is a class called InvalidPage that subclasses Page. I wrote that class as a way to prevent users from going into illegal servlet paths. At least the users are informed that the path they are trying to go is not available. Details of other classes that subclass Page are followed after the demonstration of web.xml. This is a file you cannot ignore when you are developing web application with Tomcat Apache.After that, I create a folder called "templates" in the com.pages package. This is where all the st files are located. The next thing to focus is the Page class and the subclasses (LandingPage, LoginPage and InvalidPage). The code shown below demonstrates to you the implementation of the super class Page and the introduction of StringTemplate and StringTemplateGroup.Page class gets an instance of "templateLayout" from the folder "template". "templateLayout" serves as a skeletal or a building block of dynamic content in a web page. I include all four necessary st files for our reference in this discussion. The first st file is called "templateLayout.st", followed by "landingPage.st", "loginPage.st" and "invalidPage.st"Last but not least, here are the implementations of LandingPage, LoginPage and InvalidPage classes.For a start, you may try changing the string of the titles. You do not actually need to restart the server as the changes will take effect immediately, somewhat similar to what we know as hot-deploy. I hope you have fun trying this out and please feel free to drop any comments that further improves this article :)

0 comments:

Post a Comment