Sitemesh Webapp Tutorial

Sitemesh is a page decorating framework similar to popular Tiles framework. Heres how you can compare these

  1. SiteMesh is much easier to configure and use (less typing and easier to understand)
  2. Using Tiles, you need to have your forwards go to a “tiles page” versus the direct JSP. SiteMesh takes the approach that your page (your JSP) doesn’t even know or care that it’s being decorated.
  3. Using Tiles, each individual page you want to go to has to be associated with a layout – a major point – Every time you create a new JSP that you want to forward to, you have to create another tiles definition and associate it with a layout and forward to the Tile page (versus the JSP). With SiteMesh you can simply set up a URL pattern and all your pages are automatically decorated with the layout you configure.

Disadvantages of Sitemesh
SiteMesh stores the entire content of your HTML body into memory before it decorates it. If you have some very large pages, such as might happen in a reporting application where you don’t have pagination implemented and end up with one large page of rows, you could end up with severe memory problems.
Example below demonstrates how you can configure sitemesh in your web application.

1. Project Layout


2. Web.xml

We configure sitemesh filter com.opensymphony.sitemesh.webapp.SiteMeshFilter in project’s web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns=""






3. sitemesh.xml

Create a sitemesh.xml file as below in your web application’s WEB-INF directory next to your web.xml file.

	<property name="decorators-file" value="/WEB-INF/decorators.xml" />
	<excludes file="${decorators-file}" />

		<parser content-type="text/html"
			class="com.opensymphony.module.sitemesh.parser.FastPageParser" />

			<param name="config" value="${decorators-file}" />

4. decorators.xml

Create another file decorators.xml inside your WEB-INF directory next to sitemesh.xml as below.
Whatever path pattern is provided inside <excludes>...</excludes> is ignored by sitemesh, so paths of static items like images, stylesheet, javascript etc should be put between your excludes tag.
There can be multiple layouts in your website, for example there can be a diffent layout to users that have not logged-in and a different layout for securely logged-in users. So we define multiple decorators for multiple layouts and give them a name and path of the layout jsp page that defines the layout for this decorator. Inside our decorator tag we have a pattern tag that defines url patterns where current layout gets applied.

<?xml version="1.0" encoding="ISO-8859-1"?>
<decorators defaultdir="/WEB-INF/decorators">

	<decorator name="layout" page="layout.jsp">
	<decorator name="fullPage" page="fullPageLayout.jsp">
	<decorator name="headerFooterOnly" page="headerFooterLayout.jsp">

5. layout.jsp

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

<%@ taglib uri=""

<%@page contentType="text/html; charset=UTF-8"%>

<html xmlns="" xml:lang="en">


<title> | <decorator:title
	default="Sitemesh Example" /></title>

<link rel="stylesheet" type="text/css" href="assets/css/styles.css">

<decorator:head />


<table class="mainTable" id="page-container" cellpadding="5" cellspacing="0" border="1"
		<td colspan="2" id="page-header"><%@ include
		<td id="nav-container" colspan="2"><%@ include
		<td id="left-nav-container"><%@ include
		<td id="content-container"><decorator:body /></td>
		<td colspan="2" id="page-footer"><%@ include
<table align="center">
		<td align="center"><br /><a
			href=''>Source code available on Bharat's Blog</a></td>

Above layout.jsp page defines the layout of my output on browser. I have a header, top navigation, left navigation column, footer and the main content area which is defined by <decorator:body/> tag. sitemesh automatically decorates all your pages around your content page (Cool!).
You can have several layout jsp pages defined based on different layouts your maight want in your application.
Here is how the output looks on the browser

6. Project Source Code

Download Example Source code


, , , ,

  1. #1 by Sheetal on August 16, 2013 - 6:09 AM

    Hi When I am trying to work on this source code , I am getting this error
    org.apache.jasper.JasperException: The absolute uri: cannot be resolved in either web.xml or the jar files deployed with this application

    • #2 by Bharat Sharma on August 16, 2013 - 6:35 PM

      this means that the uri could not be found in the jar which is in your classpath.
      There is a source code download link at the end of blog post. download the zip file and import code in eclipse.
      you need to have maven installed to use this code..
      on command prompt goto your project’s root directory and run following commands
      mvn eclipse:eclipse -Dwtpversion=2.0
      mvn clean install

      if you have maven eclipse plugin configured in your eclipse then right click on your project’s root directory and goto maven -> update project.
      and then run project Root -> run – > Maven Install

      to know more about maven installation see this post

  2. #3 by Anil KUmar on May 11, 2015 - 10:50 AM

    Nice post for Biginners

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: