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
In this blog post we are going to learn how to nest and extend tiles definitions. If you are new to tiles I recommend you read
this blog post first. Sometimes it is useful to have a structured page with,
say, a structured body. Typically, there is a main layout (for example, the “classic” layout) and the body is made of certain number
of sections. In this case, nesting a definition (the one for the body) inside another definition (the main layout) can be useful.
You can follow along this guide or get the final example in the github repository.
In this guide we will use Maven to manage the dependencies. Let’s add the following to our pom.xml:
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 given as properties to ease version management:
Nesting Definitions
Sometimes it is useful to have a structured page with, say, a structured body. Typically, there is a main layout (for example,
the “classic” layout) and the body is made of certain number of sections. In this case, nesting a definition (the one for the
body) inside another definition (the main layout) can be useful.
Named Subdefinitions
Tiles supports nesting definitions natively. One way of using nested definitions is creating a named “subdefinition” and using
it as an attribute. For example:
In the above snippet we have nested the myapp.homepage.body definition inside myapp.homepage, by putting it
inside its body attribute. We will be seeing the definition one inside the other.
Anonymous Nested Definitions
What you can do with named subdefinitions can be done with nested anonymous definitions. The above example can be rewritten in:
The anonymous definition put under the body attribute can be used only by the surrounding definition. Moreover, you can nest a
definition into a nested definition, with the desired level of depth.
Cascaded Attributes
Attributes defined into a definition can be cascaded to be available to all nested definitions and templates. For example the
definition detailed above can be rewritten this way:
The template of myapp.homepage.body definition has been used as the body attribute in the myapp.homepage definition.
All of the attributes of myapp.homepage.body has been then moved as attributes of myapp.homepage definition, but with
the addition of the “cascade” flag.
Our /layouts/home_body.jsp will contain this content:
Extending Definitions
You can extend definitions like a Java class. The concepts of abstract definition, extension and override are available.
Abstract Definition
It is a definition in which the template attributes are not completely filled. They are useful to create a base page and a
number of extending definitions, reusing already created layout. For example:
Definition Extension
A definition can inherit from another definition, to reuse an already made (abstract or not) definition:
In this case, the header, menu, body, footer and navigation are inherited from the myapp.page definition, while the rest
is defined inside the “concrete” definition.
Template and Attribute Override
When extending a definition, its template and attributes can be overridden.
Overriding a Template
The definition has the same attributes, but its template changed. The result is that the content is the same,
but the layout is different.
Overriding Attributes
In this case, the page will have the same appearance as the myapp.homepage definition, but its heading subpage is different.
Putting it all Together
We will first create our welcome page in the layouts folder (/layouts/classic.jsp):
Our next jsp template is the banner.jsp in the tiles folder (/tiles/banner.jsp):
/tiles/blog_header.jsp:
Conclusion
In this blog post, we have looked at how to extend and nest tiles definition. As usual you can find the source to this guide
in the github repository. Until the next post, keep doing cool things .