dagblog.
← Archive

Tiles — Smart templates for JSP


When developers build a web application’s user interface, they have to
separate application logic from presentation logic. Separating presentation
from application logic is such well-trodden ground that I’ll leave it aside here.

A second problem, just as important when building a GUI,
is separating the content from its layout. I’m talking about something other than the presentation
properties that CSS lets us separate.

Sooner or later the page layout will need
to change. In production, that often comes from a specific customer request,
such as integrating the web application into a portal, intranet or extranet. In those
cases we have to face facts and change the layout.

“Change the layout” sounds simple. Think about what it takes. Alongside purely technical problems
such as maintaining several GUI versions in parallel,
you have to rewrite a lot of presentation code. These problems come from mixing
content with layout, as web applications so often do.

The answer is to separate content
from layout strictly, using templates.
What exactly is a template? It’s simply a structure that
can be copied.

In a template we define the structure, for example
the layout of page components such as controls, the header,
data area and navigation. Every page copies
the template and supplies its own content, such as a list of orders.

Templates make it easy to break a page into individual pieces and put it back together, so
you can change its layout in one place. The individual pieces can
be reused within one application or across applications, cutting down on duplicate code.

When I first thought about this article, I thought of the
SMARTY template system, which Štěpán Kouba describes
in SMARTY — Smart Templates for PHP
on Interval.cz.

Looking back, SMARTY isn’t quite the kind of template system I mean.
It’s more about separating application logic from presentation logic. That’s
a basic premise, but it doesn’t give us the separation of layout
from content.

What fully met my expectations was Tiles,
part of the Struts MVC framework.
Tiles does much more than I’ve described, including
internationalization and XML template definitions. If you’re looking for a template system
for JSP, Tiles may be the right choice. If you’re unsure, I recommend
Chuck Cavaness’s series, starting with
Programming Jakarta Struts: Using Tiles, Part 1.

Medium version

Related articles

Related CZ Podcast episodes