Wednesday, 12 September 2012

Spring Sitemesh Integration Example

This post is a follow-up to the quick introduction to Sitemesh concepts. It provides a simple example of Sitemesh integration in a simple Spring application. The code is available on GitHub in the Spring-Sitemesh-Integration directory.

Configuration

We need to add the sitemesh filter configuration in the web.xml file. We apply the filter to all pages:
<filter>
    <filter-name>sitemesh</filter-name>
    <filter-class>
        com.opensymphony.module.sitemesh.filter.PageFilter
    </filter-class>
</filter>

<filter-mapping>
    <filter-name>sitemesh</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
We create a WEB-INF/sitemesh.xml configuration file. We specify the location of the decorators.xml file, together with the page parser and a delivered decorater mapper:
<sitemesh>
    <property name="decorators-file" value="/WEB-INF/decorators.xml"/>
    <excludes file="${decorators-file}"/>
    <page-parsers>
        <parser default="true"
            class="com.opensymphony.module.sitemesh.parser.FastPageParser"/>
        <parser content-type="text/html"
            class="com.opensymphony.module.sitemesh.parser.FastPageParser"/>
    </page-parsers>

    <decorator-mappers>
        <mapper class="com.opensymphony.module.sitemesh.mapper.ConfigDecoratorMapper">
            <param name="config" value="${decorators-file}"/>
        </mapper>
    </decorator-mappers>
</sitemesh>
We create a WEB-INF/decorators.xml configuration file to specify our decoration JSP page:
<decorators defaultdir="/decorators">
    <decorator name="default" page="MyDecorator.jsp">
        <pattern>/*</pattern>
    </decorator>
</decorators>

Decorator

We define our decorator JSP page, which adds a default header and footer. It also retrieves the decorated document's title, head and body:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %>
<!DOCTYPE html>
<html>
<head>
    <title><decorator:title default="SiteMesh Integration"/></title>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <decorator:head/>
</head>
<body>

    <div id="header">
        <h1>Header Added By Sitemesh</h1>
    </div>

    <div id="content">
        <decorator:body/>
    </div>

    <div id="Footer">
        <h2>Some Copyright Added By Sitemesh</h2>
    </div>

</body>
</html>

Pages To Decorate

We define a simple Home.jsp page:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Home Page !!!</title>
    </head>
    <body>
        This is the home page content !!!<br /><br />
        <a href="<c:url value='/AnotherPage'/>">Another Page</a>
    </body>
</html>
...with a link to AnotherPage.jsp:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Another Page !!!</title>
    </head>
    <body>
        This is the content of another page !!!
    </body>
</html>

Maven Dependency

We need the following dependency:
 <dependency>
    <groupId>opensymphony</groupId>
    <artifactId>sitemesh</artifactId>
    <version>2.4.2</version>
</dependency>

Running The Example

Once compiled, the example can be run with mvn tomcat:run. Then, browse: http://localhost:8282/spring-sitemesh-integration/.

The home page will display:

The other page displays:
More Spring related posts here.