SiteMesh > Tiles

Up to this point, most of the Java web applications I've built have used the open source Tiles project from Jakarta to control page layouts. Although I've typically been able to get it to turn out the results I wanted, Tiles still leaves some things to be desired. Recently, I've been introduced to SiteMesh from OpenSymphony on my current project. The bottom line is that SiteMesh is a Tiles killer. You get all the functionality of Tiles without the headaches. It is easier to learn, setup, configure, maintain, and it even seems to perform much better (no scientific tests performed...just initial observations).

My Approach
I came up with a set of decorators [combined with our CSS stategy] to control page layout. I've listed them here with a brief description of each:
  • normal.jsp - Flexible layout, variable width, include branding, tools, nav,...

  • portal.jsp - Fixed width, used to layout pages in portal style, includes branding, tools, nav, ...

  • light.jsp - Light branding, no tools, no navigation. Used on pages like login.jsp where users haven't been authenticated yet

  • minimal.jsp - No branding or anything, but full xhtml document. Good for dialogs

  • error.jsp - Intended to decorate error pages only

  • portlet.jsp - Wraps content inside a portlet look. Not a full xhtml document; only a fragment

  • stripped.jsp - Uses no decoration at all, only includes body. Good for pages where you can't have any extras. Basically, this takes "minimal.jsp" one step futher.

The Good (Pros)
I want to point out the highlights of SiteMesh's strengths.
  • Central configuration - There is just ONE place to do all layout configurations (decorators.xml). And because of patterns (see next point) you don't have to create a definition for EVERY page in your application.

  • Patterns - to apply a decorator to a page, you just have to have a matching pattern definition in decorators.xml (including the use of wildcards)

  • Applying decorators inside decorators - much like Tiles, you can always apply a decorator to a portion of another decorator. I found this useful when I needed to apply my "portlet" decorator to just a portion of a page. You can even pass parameters to the "subdecorators" just like you can in Tiles.

  • Flexibility - you can do just about anything you dream up. Everytime I dreamed up some strange requirement, I was able to pull it off...without the typical kludginess. This one surprises me, because SiteMesh's tag library consist of like 6 tags total. SiteMesh is simple, but still very flexible.

  • Full decorators vs. fragments - your decorators can be fully valid xhtml pages, or they can be small fragments that belong in a bigger page.

  • Useful tag attributes - the writeEntireProperty attribute of the decorator:getProperty prevents you from having to do silly empty checks and conditional insertions of non-breaking spaces.

  • Layouts separate from content - you can finally keep your dynamic pages content-only! Use the decorators in combination with cascading stylesheets to style your markup. Keep your content clean!

  • Pages can live on their own - You can build your jsps as fully functional on their own. You don't have to make them fragments and assume they'll be properly wrapped with navigation, branding, footer, menu, ... by some other entity. Because SiteMesh rips out all the head and body tags you have ultimate control. Ultimately, this can help you reduce the total number of jsp files.

The Not-So-Good (Cons)
Of course, nothing's perfect. Here's a few complaints I have with SiteMesh:
  • Error pages - Error pages (404, 500, ...) don't get decorated according to patterns you define in decorators.xml. There's a workaround (apply the decorator locally in the error jsp). This feels wrong (I like to keep my pages from knowing they are being decorated), but maybe there's a reason for this that I'm aware of (i.e. response headers?)

  • Compile Errors - because of the previous point, compile errors can get really messy...especially when you use a "subdecorator" technique. Because you have to apply decorators from within error pages, you can end up w/ double branding, navigaiton, menus, etc. I suppose this is ok, since you should clean up those compile errors prior to deployment, but it'll give you a jolt during development.

  • Infinite Looping - if I had a problem with one of my jsps, I'd occasionally see some infinite looping issues. I can't for sure say it was SiteMesh's fault (we were also using Spring MVC), but it appeared to be the culprit. Page text prior to the error was repeated over and over on the page infinitely. Sorry, I'm fuzzy on the details of this one, but it was all related to a syntax problem on the jsp that was being decorated.

  • Third-party custom tag libraries - We were using a DB2 Alphablox tag that had to be placed inside the decorators. The tag does some server-side mojo that required us to move it to the decorator. I would rather have left the tag in the original jsp, but SiteMesh was ignoring the tag altogether.

In summary, I really like SiteMesh. I suppose you can tell by the list above. If I ever discover any additional shortcomings I'll be sure to share those, too.


Tom Pierce at: 12:12 PM said...

Great article and I totally agree with your assessment, Aaron.

Minor correction on the third party tags: It wasn't filtering out the JSP tags, it was filtering out the HTML and JavaScript generated by the tag because it was in the head of the decorated page.

Unknown at: 1:50 PM said...

Thanks for the correction, Tom.

Anonymous at: 1:44 AM said...

Has the situation with sitemesh and 404s improved any?

Unknown at: 3:42 PM said...

I don't know. Unfortunately, I haven't had an opportunity to work with Tiles since my original post. If you find out, please let me know.

Anonymous at: 12:34 AM said...

what is your experience with sitemash and memory problems, as sitemash will put entire html output into the memory?

John Lockwood at: 2:51 PM said...

I configured Spring MVC to use tiles over the weekend and found that process to be fairly frustrating. I just did the same thing for Sitemesh and it was a piece of cake, and everything I tried pretty much just worked. So I agree with your assessment -- easier to begin, and easier to maintain!



Post a Comment

Aaron Hursman
Aaron Hursman is a passionate user-advocate who is lucky enough to do what he loves for a living. As a user experience architect, he applies user-centered design principles and techniques including user research, persona development, information architecture, storyboards, wireframes, prototyping, visual design, graphic design, interaction design, and usability. Aaron has a background in web development, enterprise applications, and the social web. At nGame, he is applying his craft to design and build the next generation of enterprise software. Aaron is available as a speaker and author upon request.
Disclaimer: The information in this website is provided "as is" with no warranties, and confers no rights. This website does not represent the thoughts, intentions, plans or strategies of my employer. It is solely my own personal opinion. Inappropriate comments will be deleted at the authors discretion. All instructions and code samples (if any, ever) are provided "as is" without warranty of any kind, either express or implied.