The Apache Project

Tiles Guide

The Tiles Document Assembly Framework

Tiles builds on the "include" feature provided by the JavaServer Pages specification to provide a full-featured, robust framework for assembling presentation pages from component parts. Each part ("Tile") can be reused as often as needed throughout your application. This reduces the amount of markup that needs to be maintained and makes it easier to change the look and feel of a website.

Overview of Tiles Features

  • Screen definitions
    • Create a screen by assembling Tiles , e.g. header, footer, menu, body
    • Definitions can take place:
      • in a centralized XML file
      • directly in JSP pages
      • in Struts Actions
    • Definitions provide an inheritance mechanism: a definition can extend another one and override some (or all) of its parameters
  • Layouts
    • Define common page layouts and reuse them across your web site
    • Define menu layouts and pass lists of items and links
    • Define a portal layout, use it by passing list of Tiles (pages) to show
    • Reuse existing layouts, or define your own ones
  • Dynamic page building
    • Tiles are gathered dynamically during page reload. It is possible to change any attributes: layout, list of Tiles in portal, list of menu items, ...
  • Reuse of Tiles /Components
    • If well defined, a Tile can be reused in different locations
    • Dynamic attributes are used to parameterize Tiles
    • It is possible to define libraries of reusable Tiles .
    • Build a page by assembling predefined components, give them appropriate parameters
  • Internationalization (I18N)
    • It is possible to load different Tiles according to the user's Locale
    • A mechanism similar to Java properties files is used for definition files: you can have one definition file per Locale, the appropriate definition is loaded according to the current Locale
  • Multi-channels
    • It is possible to load different Tiles according to a key stored e.g. in session context
    • The key could hold e.g. user privileges, browser type, ...
    • A mechanism similar to Java properties files is used for definition files: you can have one definition file per key, the appropriate definition is loaded according to the key

Enabling your application for Tiles

The Tiles framework is bundled with Struts but not enabled by default. To enable Tiles you need to:

  • Setup the struts-tiles taglib in your WEB-INF/web.xml file to include the following tag library declaration:
  • At the top of each JSP page that will use the Tiles custom tags, add the following line declaring the Tiles custom tag library for use on the page:
<%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles" %>
Servlet 2.3: You can omit the declaration in WEB-INF/web.xml and replace above line with the full URI:
<%@ taglib uri="" prefix="tiles" %>
  • If you plan to use Tiles definitions defined in a centralized file, you need to create this file and instruct Struts to load the Tiles plugin which will create the factory corresponding to the file. You can have more than one definitions file.
    • Create a file containing your definitions (e.g. WEB-INF/tiles-defs.xml). You can use the tiles-defs.xml file from the Tiles application for a detailed example of the required syntax.
    • Setup the Tiles plugin in each struts-config.xml file corresponding to a module:
    <plug-in className="org.apache.struts.tiles.TilesPlugin" >
      <set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml,
    /WEB-INF/tiles-tests-defs.xml" />
      <set-property property="definitions-parser-validate" value="true" />
      <set-property property="moduleAware" value="true" />
    • Note: This plugin creates one factory for each Struts modules. The plugin first reads the factory parameters from web.xml and then overloads them with the ones defined in the first struts-config.xml file.
  • Note: The Tiles framework now uses the commons-logging package to output different information or debug statements. Please refer to this package documentation to enable it. The simplest way to enable logging is to create two files in WEB-INF/classes:
    # Logging detail level,
    # Must be one of ("trace", "debug", "info", "warn", "error", or "fatal").

Tiles API Guide

Tiles Resources

Reuse Tiles and Simplify UI by James Holmes. Howto article in Oracle Magazine.

Developing applications with Tiles by Cedric Dumoulin and Ted Husted. Sample chapter from Struts in Action; available as a free download (PDF).

Using Tiles Sample beta chapter from Programming Jakarta Struts; available as a free download (PDF).

Struts and Tiles aid component-based development by Wellie Chao.

UI design with Tiles and Struts by Prakash Malani.

Tiles 101/201 by Patrick Peak.

Tiles Advanced Features by Cedric Dumoulin.