This tutorial is a quick introduction to using SiteMesh3 in a web-application. It covers:
- A high level overview of how SiteMesh3 works
- Installation and configuration
- Building and applying a simple decorator
It is recommend you read the high level SiteMesh 3 Overview before this tutorial.
In a web application, SiteMesh acts as a Servler Filter. It allows requests to be handled by the Servlet engine as normal, but the resulting HTML (referred to as the content) will be intercepted before being returned to the browser.
The intercepted content has certain properties extracted (typically the contents of the <title>, <head> and <body> tags and is then passed on to a second request that should return the common look and feel for the site (referred to as the decorator). The decorator contains placeholders for where the properties extracted from the content should be inserted.
Under the hood, a key component of the SiteMesh architecture is the content processor. This is an efficient engine for transforming and extracting content from HTML content. For most users, it's fine to use it as it comes, but it is also possible to define your own transformation and extraction rules.
SiteMesh does not care what technologies are used to generate the content or the decorator. They may be static files, Servlet, JSPs, other filters, MVC frameworks, etc. So long as it's served by the Servlet engine, SiteMesh can work with it.
Running SiteMesh3 requires at least:
- JDK 1.5
- A Servlet 2.5 compliant container
- The SiteMesh runtime library
The SiteMesh library should be downloaded and placed in /WEB-INF/lib/.
Insert the SiteMesh Filter in /WEB-INF/web.xml:
Deploy the web-application to your Servlet container. From this point onwards, this tutorial assumes the web-app is running at http://myserver/.
The decorator contains the common layout and style that should be applied to the pages in the web application. It is a template that contains place holders for the content's <title>,<head> and <body> elements.
At the bare minimum, it should contain:
The <sitemesh:write property='...'/> tag will be rewritten by SiteMesh to include properties extracted from the content. There are more properties that can be extracted from the content and it's possible to define your own rules - that will be covered in another tutorial.
The bare minimum decorator isn't very useful. Let's add some style and a bit of common layout.
Create the file /decorator.html in your web-app, containing:
In this example, the decorator is a static .html file, but if you want the decorator to be more dynamic, technologies such as JSP, FreeMarker, etc can be used. SiteMesh doesn't care - it just needs a path that can be served content by the Servlet engine.
SiteMesh needs to be configured to know about this decorator and what it should do with it.
The configuration file should be created at /WEB-INF/sitemesh3.xml:
This tells SiteMesh that requests matching the path /* (i.e. all requests) should be decorated with /decorator.html that we just created.
If you don't like the idea of having to use XML to configure SiteMesh, don't worry - there are alternative mechanisms including directly in WEB-INF/web.xml, programatically through a Java API, through Spring, by naming convention, or any custom way you may choose to plug in. These are explained further in another article.
Now to create some content. This is defined in plain HTML content. Create /hello.html:
Like the decorator, the content may be static files or dynamically generated by the Servlet engine (e.g. JSP).
Pointing your browser to http://myserver/hello.html will serve the content you just created, with the decorator applied. The resulting merged HTML will look like this:
As you can see, the <title>, <head> and <body> have been extracted from the content and inserted into the decorator template.
A quick recap:
- SiteMesh is installed by dropping the library jar in /WEB-INF/lib and creating a filter (with mapping) in /WEB-INF/web.xml
- It can be configured by creating a /WEB-INF/sitemesh3.xml file, or through other configuration methods
- The filter intercepts requests to Content, runs it through the Content Processor and merges with a Decorator
- The Content is defined by an HTML page, that contains the vanilla HTML content of the site
- The Decorator is also defined by an HTML page, that contains the look and feel of the site, and placeholder <sitemesh:write> tags to indicate where the Content should be merged in
- The Content Processor contains the rules for extracting and transforming the content - it has some simple default rules and can be customized