Apache Struts 2 Documentation > Home > Guides > Plugin Developers Guide > SiteMesh Plugin
Added by Patrick Lightbody, last edited by Piero Sartini on Jul 14, 2008  (view change)
About SiteMesh

SiteMesh is a web-page layout and decoration framework and web application integration framework to aid in creating large sites consisting of many pages for which a consistent look/feel, navigation and layout scheme is required.

The Sitemesh plugin allows Sitemesh templates to access framework resources.

The framework stores all its value stack information as request attributes, meaning that if you wish to display data that is on the stack (or even the ActionContext), you can do so by using the normal tag libraries that come with the framework. That's it!

Features

  • Can use Struts tags in Sitemesh decorator templates

Usage

ActionContextCleanUp

Under the framework's architecture, the standard filter-chain optionally starts with the ActionContextCleanUp filter (or StrutsPrepareFilter since 2.1.3), followed by other desired filters. Lastly, the FilterDispatcher (or StrutsExecuteFilter) handles the request, usually passing it on to the ActionMapper. The primary purpose of the ActionContextCleanUp is to provide SiteMesh integration. The clean-up filter tells the dispatcher filter exactly when to remove obsolete objects from the request. Otherwise, the ActionContext may be removed before the decorator attempts to access it.

Order Matters
If ActionContext access is required within the decorators, the ActionContextCleanUp filter must be placed at the beginning of the filter-chain.
Javadoc: (org.apache.struts.action2.dispatcher.ActionContextCleanUp)
Struts 2.1.3
Since Struts 2.1.3, ActionContextCleanUp and FilterDispatcher are deprecated. Use StrutsPrepareFilter and StrutsExecuteFilter instead.

FreeMarker and Velocity Decorators

The plugin provides an extension of the SiteMesh PageFilter to assist with integration with Velocity and FreeMarker. Our filters provide the standard variables and Struts Tags that you used to create views in your favorite template language.

FreeMarker

The FreeMarkerPageFilter extends the SiteMesh PageFilter to allow direct access to framework variables such as $stack and $request.

In the web.xml, the VelocityPageFilter should be placed between the ActionContextCleanUp and the FilterDispatcher.

Javadoc: (org.apache.struts2.sitemesh.FreeMarkerPageFilter)

The following variables are available to the decorating freemarker page :-

  • ${title} - content of <title> tag in the decorated page
  • ${head} - content of <head> tag in the decorated page
  • ${body} - content of t<body> tag in the decorated page
  • ${page.properties} - content of the page properties

With the following decorated page :-

 <html>
     <meta name="author" content="tm_jee" />
     <head>
         <title>My Title</title>
         <link rel="stylesheet" type="text/css" href="mycss.css" />
         <style type="text/javascript" language="javascript" src="myjavascript.js"></script>
     </head>
     <body<
         <h1>Sample</h1>
     </body>
 </html>
 

Properties Content
${title} My Title
${head} <link rel="stylesheet" type="text/css" href="mycss.css" /> <style type="text/javascript" language="javascript" src="myjavascript.js"></script>
${body} <h1>Sample</h1>
${page.properties.meta.author} tm_jee

Velocity

The VelocityPageFilter extends the SiteMesh PageFilter to allow direct access to framework variables such as $stack and $request.

In the web.xml, the VelocityPageFilter should be placed between the ActionContextCleanUp and the FilterDispatcher.

Example

Here is an example of how to configure the filter chains in web.xml:


Settings

This plugin doesn't support any global settings.

Installation

This plugin can be installed by copying the plugin jar into your application's /WEB-INF/lib directory. No other files need to be copied or created.