less than 1 minute read

If you have used other MVC frameworks (Rails or Grails for example) you will miss layout templates from Spring. I found an easy-to use solution which is also used in Grails under the hood. If you use Maven in your project, simply add it to the pom.xml file under the dependencies:

<dependency>
    <groupId>opensymphony</groupId>
    <artifactId>sitemesh</artifactId>
    <version>2.4.2</version>
</dependency>

Next, add the following to your web.xml:

<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>

Add a new decorators.xml file under the WEB-INF directory:

<decorators defaultdir="/WEB-INF/layouts">
    <decorator name="application" page="application.jsp">
        <pattern>/*</pattern>
    </decorator>
</decorators>

This means /WEB-INF/layouts/application.jsp will be used for every url your application catch. You can specify others, for example /admin/* or /user/*:

    <decorator name="admin" page="admin.jsp">
        <pattern>/admin/*</pattern>
    </decorator>
    <decorator name="user" page="login.jsp">
        <pattern>/user/*</pattern>
    </decorator>

Now. you need to create the layout file(s) under /WEB-INF/layouts directory. Use the following code in it:

<%@ taglib prefix="decorator" uri="http://www.opensymphony.com/sitemesh/decorator" %>
...html, head, title, etc...
<nav>
<decorator:navigation/>
</nav>
<decorator:body/>

will be replaced as specified in the views:

<body>
   <h1>The title of an article</h1>
   <p>The body of the article</p>
</body>

Visit the official SiteMesh website for more specified examples: http://www.opensymphony.com/sitemesh/

Updated:

Comments