Smart Tech for a better Web
Struts 2.2.1 is using Sitemesh 2.4.2. Sitemesh is a very good but basic templating framework using the Decorator design pattern. Unfortunately Sitemesh 2 is discontinued and not really maintained anymore - even the latest release cannot be found on the www.opensymphony.com/sitemesh. The new Sitemesh website is targeting on version 3. I have refused to upgrade my Struts just to use some of the new features. Instead I tried to solve my little problem with a git clone of the Sitemesh 2 code.
Usually you use the Sitemesh body tag to get out the whole content of the body which will then be decorated by the Decorator:
But if you have a two column design and want to avoid page includes and two jsps in the style of content1Sidebar.jsp and content1Mainpanel.jsp you are lost. What if you don’t want a sidebar? Create an empty jsp file? Bah.
Instead you can try to decorate a jsp of that kind:
Decorating this is trivial - if you know the DivExtractingPageParser. If you have bootstrapped your Struts App with mvn archetype, you probably already have a sitemesh.xml. It contains a section like this:
Well, on a sidenote: the FastPageParser is deprecated and should be replaced with the HTMLPageParser, if you want to use the body tag.
But we want something else - add the DivExtractingPageParser:
This parser extends the HTMLPageParser. It will draw the first level divs in your HTML and store it into properties. In this example you can use them in your Decorator (in my case the main.jsp) with:
And that’s it. Don’t forget to remove this tag:
And you’ll need to create divs for every jsp file you want to decorate.