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
- 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
- Definitions provide an inheritance mechanism: a definition can extend another one and override some (or all) of its parameters
- 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, ...
- 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
- 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
- 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.xmlfile to include the following tag library declaration:
<taglib> <taglib-uri>/WEB-INF/struts-tiles.tld</taglib-uri> <taglib-location>/WEB-INF/struts-tiles.tld</taglib-location> </taglib>
- 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.xmland replace above line with the full URI:
<%@ taglib uri="http://struts.apache.org/tags-tiles" 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.xmlfile from the Tiles application for a detailed example of the required syntax.
Setup the Tiles plugin in each
struts-config.xmlfile 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" /> </plug-in>
Note: This plugin creates one factory for each Struts modules. The plugin first reads the factory parameters from
web.xmland then overloads them with the ones defined in the first
- Create a file containing your definitions (e.g.
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
commons-logging.properties org.apache.commons.logging.Log=org.apache.commons.logging.impl.SimpleLog simplelog.properties # Logging detail level, # Must be one of ("trace", "debug", "info", "warn", "error", or "fatal"). org.apache.commons.logging.simplelog.defaultlog=trace
Tiles API Guide
Reuse Tiles and Simplify UI by James Holmes. Howto article in Oracle Magazine.
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.