Apache Tiles is a free open-sourced templating framework for modern Java applications. Based upon the
Composite pattern it is built to simplify the development of user interfaces.
Introduction
Building web applications most often than not requires duplicating common page elements across several pages. When adding links to page
navigation for instance, one is required to copy/paste these links on all pages that have navigation. This can sometimes prove to be
repetitive and wrought with errors.
To avoid code duplication, developers look for tools and/or libraries to make this easier. Shared page elements can be created as
autonomous units and reused across the entire web application using these units.
One of such tools that goes a step futher is Apache Tiles. Apache Tiles is a template composition framework. Tiles was originally
built to simplify the development of web application user interfaces, but it is no longer restricted to the JavaEE web environment.
For complex web sites it remains the easiest and most elegant way to work alongside any MVC technology. Tiles allows authors to define
page fragments which can be assembled into a complete page at runtime. These fragments, or tiles, can be used as simple includes
in order to reduce the duplication of common page elements or embedded within other tiles to develop a series of reusable templates.
These templates streamline the development of a consistent look and feel across an entire application.
The final working sample can be found in the github repository. For advanced usage of tiles refer to this
blog post.
Project Structure
At the end of this guide our folder structure will look similar to this:
The dependency on tiles-extras pulls in all transitive dependencies of tiles. For this guide, we will be using tiles in a web
environment and as such require servlet-api and jsp jars:
The versions of the various dependencies are represented as placeholders to ease version management:
Setting up Tiles
tiles-definitions are set up in a file called tiles.xml. Add the following snippet:
The places of interest to note in the above snippet is the name and template attributes of the <definition> element. The name
attribute is used to identify the tiles-definition (we would come back to this later) and the template attribute to specify
where the template file can be found relative to the servlet context i.e. src/main/webapp.
The child element <put-attribute> of parent element <definition> specifies a name/value pair in our tiles definition. In this
instance the page title.
Let us now use the definitions in our jsp template:
Notice the <tiles> child element within the <title> element. It is tagged with getAsString which retrieves the title property
(Tiles getting started homepage) from the <put-attribute>. To run this we need one more configuration bootstrap in web.xml:
Also take note of the tiles namespace on the second line (xmlns:tiles="http://tiles.apache.org/tags-tiles")
org.apache.tiles.extras.complete.CompleteAutoloadTilesListener is used to load the tiles container.
For this Guide, we have configured Tiles to work directly with the servlet API, without a controller using
org.apache.tiles.web.util.TilesDispatchServlet. In the real world, you’ll probably use an MVC framework like
Spring or Struts. You have to configure your framework to work with Tiles; please refer to your framework’s
documentation for that.
Run this in any compartible servlet 3.1 container to view the output.
Adding Attributes
Now that we have seen how to create definitions, let us add some more attributes to this definition::
The <put-attribute> elements above all reference file fragments in the src/main/webapp/tiles/ directory. We would create this
directory and add the following files to it:
Notice the <tiles> element right before the <jsp:root> closing tag, we are calling the tiles-definition by name. This tells
tiles to insert the classic.jsp layout in the index page.
Run this in any servlet 3.1 container to see the final output.
When tiles dispatcher servlet is configured, you can serve definition names directly with *.tiles e.g. /myapp.homepage.tiles:
Tiles can be used with web frameworks such as Spring and Struts, and template
technologies like JSP and Thymeleaf.
You can find the source to this guide in the github repository. Until the next post, keep doing cool things .