Apache Struts 2 Documentation > Home > Guides > Tag Developers Guide > Struts Tags > UI Tags > Themes and Templates > Extending Themes
Added by Patrick Lightbody, last edited by Ted Husted on Sep 02, 2006  (view change)

Most often, an application may just need to override a template (see Template Loading) so that a certain control renders differently. Or, an application many need to add a new template to an existing theme. Other times, you might want to create an entirely new theme, perhaps because you are building a rich set of unique and reusable templates for your organization.

There are three ways to create new themes:

  • Create a new theme from scratch (hard!)
  • Wrap an existing theme
  • Extend an existing theme

Creating a New Theme from Scratch

Keep it simple, Sam!
It's probably never a good idea to create a new theme from scratch. Instead, use the simple theme as a starting point. The simple theme provides just enough foundation to make it easy to create new controls by extending or wrapping the basic controls. Before starting a new theme, be sure to review the source templates for all of the provided themes. The existing themes are your best guide to creating new themes.

Wrapping an Existing Theme

The xhtml theme provides several good examples of the "wrapping" technique. The simple theme renders the basic control. The xhtml theme "dresses up" many of the controls by adding a header and footer.

Wrapping a control
<#include "/${parameters.templateDir}/xhtml/controlheader.ftl" />
<#include "/${parameters.templateDir}/simple/xxx.ftl" />
<#include "/${parameters.templateDir}/xhtml/controlfooter.ftl" />

Wrapping is a great way to augment the basic HTML elements provided by the simple theme.

Extending an Existing Theme

One benefit of object-orientated programming is that it lets us "design by difference." We can extend an object and code only the behavior that changes. Themes provide a similar capability. The subdirectory that hosts a theme can contain a theme.properties file. A parent entry can be added to the property file to designate a theme to extend. The ajax theme extends the xhtml theme using this technique.

/template/ajax/theme.properties
parent = xhtml

An extended theme does not need to implement every single template that the Struts Tags expect. It only needs to implement the templates that change. The other templates are loaded from the parent template.