Spring Mvc–Layout/Master pages

To set up layout pages in Spring you could use SiteMesh. The way you set it up is as follows:

Add the latest version of sitemesh to your pom file, during this writing it was v2.4.2

sitemesh

Create a new decorators.xml file within the WEB-INF folder, this file is used to describe where your layout page is, when it should be used. Here I am telling it that my layout.jsp page is in a folder called shared and that it should be used for any url patterns.

decorators

Now add the following two lines in your web.xml file

webxml

And finally extract all of the common UI elements into the layout.jsp file. Notice the keyword “decorator”, basically the requested page will be decorated by this layout page.

layout

And here is the hello.jsp page.  The layout’s title element will be replaced by “This is hello’s title” and everything else will be put within the body

hello