Basic Tiles Integration

Version 1.1 by Ken McWilliams on 2012/03/18 21:55

About Apache Tiles

This is background for people new to templates. If you know you want to integrate tiles skip to the next heading.

If you are here you are aware of the problem: Web pages share a lot of commonality. Being Java programmers we are well versed in the problems of code duplication (because object oriented programming has excelent tools to combat this issue we are ever vigilent), simply it makes reuse and maintinence difficult.  Two tools we use without hardly a thought in our Object Oriented arsenal is Composition and Inherietance.

Think then of a Template as an Object, which has as its properties a body and values which are inserted into that body.

If we want to reuse a template it would be nice to say: I would like to create an new template exactly like my other template, but I want to override part of it with anther implementation (that is I want to substitute the body portion with other data). If so, tiles is for you!

So we can think of a template as being like a Java Object. We can use composition to group templates together for complicated page layout and we can use inheritance to reuse a template by changing just the portions we want to suit our needs.

In this way we can remove all the code duplication.

With Apache Tiles we reap the following benifits (I'm assuming JSPs as the view but Freemarker and Velocity are also supported, although you may need to upgrade to tiles 2.2.2 which is the subject of another tutorial):

  • JSPs reflect only relevant content. For instance it is a lot easier to understand a page which just conserns the body content than trying to understand a page marked up for both content and presentation and vice versa it is a lot easer to understand a template that is not loaded with content.
  • It is easy to create tempates in parts. ie: You can have a template to layout the head(html head element - to bring in required java script and css resources), page header, menu, body and footer. Then you can have several templates specifically for each of the previous components.
  • Increases the separation beween designers and programmers. There should be more separation between JSPs which aquire and display data and JSPs which act as templates, the later of which the designers are more interested in. 
  • Tiles xml provides a souce from which all template definitions can be viewed at once (this is the main strength over JPS includes and will be expanded upon in the next section).

Apache Tiles vs JSP includes

Before I start there is another major player: Sitemesh.