Tiles is an implementation of the Composite View pattern. Tiles adds to this pattern its own concepts to make the pattern concrete. The implementation of Tiles around the Composite View pattern consists of the Template, Attribute and Definition concepts. The View Helper pattern is implemented by the View Preparer concept.
In Tiles, a template is the layout part of a page. You can see as a page structure with some gaps, called attributes, to be filled.
For instance, consider the "classic layout" page structure.
You can replicate this structure by creating a JSP page, as you can see below.
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %> <table> <tr> <td colspan="2"> <tiles:insertAttribute name="header" /> </td> </tr> <tr> <td> <tiles:insertAttribute name="menu" /> </td> <td> <tiles:insertAttribute name="body" /> </td> </tr> <tr> <td colspan="2"> <tiles:insertAttribute name="footer" /> </td> </tr> </table>
Notice that a template can have no attributes: in this case it can be used directly.
An attribute is a gap in a template that needs to be filled in your application. An attribute can be of three types:
A definition is a composition to be rendered to the end user; essentially a definition is composed of a template and completely or partially filled attributes.
For example, you can create a page using the classic layout as seen before, by modifying the Tiles configuration file.
<definition name="myapp.homepage" template="/layouts/classic.jsp"> <put-attribute name="header" value="/tiles/banner.jsp" /> <put-attribute name="menu" value="/tiles/common_menu.jsp" /> <put-attribute name="body" value="/tiles/home_body.jsp" /> <put-attribute name="footer" value="/tiles/credits.jsp" /> </definition>
Sometimes a definition, before it is rendered, needs to be "prepared". For example when showing a menu, the menu structure must be created and stored in the request scope.
For this reason, a View Preparer can be used: it is called before the definition is rendered, so all the things needed to render correctly the definition can be prepared.
See the Tiles View Preparer configuration for more information.