Spring MVC with layout templates

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:


Next, add the following to your web.xml:


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

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

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">
    <decorator name="user" page="login.jsp">

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

will be replaced as specified in the views:

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

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