Java Layout Frameworks for Web Pages
To make things easier when developing and maintaining this website, I decided to use a layout framework instead of simply including header and footer files in my JSP files. Sure, header and footer files would have done it as well, but with a layout consisting of a main body content area, sidebar, navigation bar and page footer, where each can differ from page to page, a layout framework helps a lot.
First of all, there’s the important decision of which layout framework to use. There are two well-known Java layout frameworks for web pages: SiteMesh and Struts Tiles.
SiteMesh is mostly about intercepting complete HTML pages to which a layout is then applied. That is, you could create JSP files containing as simple HTML code as possible and, when the page is requested, intercept it and apply your website’s layout using a decorator.
The different parts of a page that can be intercepted and inserted into a layout are quite limited; the HTML <head>, <title> and <body>. In other words, if you want to find the intercepted page’s sidebar and include it into the layout’s sidebar, you won’t be able to do it very easily. Supposedly, SiteMesh also allows inclusion of pages into layouts as panels but there doesn’t seem to be any documentation about it. However, including a small amount of text, e.g. for a footer, would be easy; in the page to be intercepted, enter the text into a <meta> tag and, in the decorator, insert a <decorator:getProperty /> tag where the footer text is to be located, e.g. <decorator:getProperty name=”meta.footer” /> for a <meta> tag with the name “footer”.
Struts Tiles, on the other hand, doesn’t intercept pages at all. Instead, for each page, you create a definition that specifies what content (e.g. a JSP file or a text string) is to be located where in your layout file (typically a JSP file). The location can be anywhere you like—it is named, by yourself, in the layout file and referenced, by the same name, in your page definitions. These definitions can be created in an XML file, in JSP files and in Struts Actions. Also, a definition can extend another definition, overriding some or all of its configuration. All this makes Struts Tiles quite a powerful but simple layout framework.
Here’s what I would do when deciding which layout framework to use: If I were starting from scratch with a relatively simple webapp, I’d go with Struts Tiles. If I were going to apply a layout to an existing webapp or integrating the layout of several webapps, I’d try doing it with SiteMesh. For larger and more complicated webapps it’d be harder—possibly either of these or something else entirely; Struts Tiles basically requires a new definition for each page which can be tough when dealing with a lot of pages and SiteMesh, as I mentioned before, is quite limited in its flexibility.
With the exception of this blog, this website obviously fits into the first category (starting from scratch with a relatively simple webapp). By creating a new theme for the blog and completely adapting it to the website (new JSP files for viewing of entries etc), I pretty much made it fit perfectly into the first category, so Struts Tiles was an easy choice. It was made even easier by the fact that I needed more flexibility than SiteMesh had to offer and that I was thinking about using Struts on my website at some point anyway.