JSP Page Design With Tiles
Tiles is a taglib packaged with Jakarta Struts 1.1 (currently in beta stage, but quite
stable) and is available as part of the struts package. Struts
is an open-source Web
application framework available from http://jakarta.apache.org/struts.
Tiles lets the user break the presentation page into small JSP snippets that perform a
specific function but are not complete pages in their own rights. Each such
snippet becomes known as a tile. Putting these tiles together creates a
The tiles mechanism has advantages over the basic
jsp:include tag in that it
is dynamically configurable: an application layout can be defined and portions of it
filled in dynamically -- based on values that become known at runtime (such as the current
Locale settings, for example).
Tiles can be used on its own; however, it can also be used from within the struts
framework. This document will create a simple set of pages with tiles to teach by
example; these pages will share a common header, footer and basic layout.
Initial Tiles Installation and Setup
Tiles consists of one taglib (
struts-tiles.tld); all the supporting Java
classes are contained within the main struts JAR (
A standard taglib entry is placed inside the configuration file (
the Web application:
|Definition of Tiles taglib in
It is possible to store Tiles definitions inside a centralized XML file (the approach
taken in this document). In this case, Tiles needs to be installed as a struts plugin;
this basically installs the Tile factory as a plugin, enabling the contents of the tiles
definition file to be processed.
<set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml"/>
<set-property property="definitions-debug" value="1"/>
<set-property property="definitions-parser-details" value="0"/>
<set-property property="definitions-parser-validate" value="true"/>
|Definition of Tiles plugin in
Note that the value of the
definitions-config property can be a
comma-separated list of tile definition files in the event that there are more than
To use Tiles in a JSP, simply use the standard taglib tag:
<%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles" %>
|Preparing for use of the Tiles taglib inside a JSP
Designing a main tiles layout for use as the page template consists of creating JSP
page with placeholders where other pages, tiles layouts or literal strings (such as page
titles) will eventually be placed.
We will use layout.jsp for this
application's look. (The source is available here.)
This page basically defines a look for the pages in the application: a title, a header
page, a heading, a main body and a footer. The contents of the page will be indented
slightly (by being contained in a table) for effect. A breakdown of the significant
lines in the source is as follows:
||Identifies the Tiles taglib to be used on this page.
||Sets the window title based on dynamically specified text -- varies from page to page.
||Inserts the content of the dynamically specified header page -- this will probably
be the same on every page but may vary as the name of the page is not hard-coded here.
Contains common elements like a top menu.
||Displays the dynamically specified text as a page heading -- varies from page to
page and is probably just a longer version of the title.
||Inserts the contents of the dynamically specified body -- this is where the actual
contents of the page in question will go (such as a menu page consisting of anchors or a
login form for validation).
||Inserts the contents of the dynamically specified footer page -- behaves quite
similarly to the header and contains common page closing tags such as company
This is the
/WEB-INF/tiles-defs.xml file (source)
mentioned in the
A description of some of the key lines:
||The main element of a tiles defintion file is
||The definition of a layout named
mainLayout that will be used by the
pages in the application for their appearance. The page corresponding to the layout
is defined as layout.jsp.
||These are values that will be placed into the resulting page as literals (and not
interpreted as tile JSP pages); individual pages will obviously override these to suit
their page title and headings. These correspond to the
getAsString described earlier.
|13, 15, 17
||These values will be considered to point to either other Tiles layouts or specific
JSP pages. (The system will basically try to match the name to a layout; if that
fails, it will look for a page with the name instead.) Of these, only the
body value is expected to change on a per-page basis, though some pages
might want a different header or footer (say, to display user information if the user
has logged in). These values correspond to the
tiles tag discussed earlier.
Values specified using the
put tag can be used either as strings or
as pages (or layouts); it is entirely up to the page that uses the values to get
them as strings or pages.
Tiles does in fact allow for setting individual values explicitly as strings for
stronger typing purposes (the
put tag contains a
attribute that is optional but can be used for explicit typing); the generic way
is simply easier. However, if a layout is going to shared across multiple
developers, the stronger typing is important to ensure that the attributes are
used as intended.
Note that these are merely the default values. Some of these will be changed on a
per-page basis while others (such as the header) may be left to the defaults.
There can be several such layouts, allowing for different functionality throughout the
application and the values that point to individual pages could just as easily be the
names of another such layout defined in the configuration files.
Up to now, we have defined a Tiles layout. There aren't, however, any pages that make
use of this layout. Before we can create those, we have to create a simple header and
footer for use in our final version. The source for header.jsp is available here and the source
for footer.jsp is available here. Note how these pages are not completely formed HTML
pages -- they are tiles which need to be placed inside a larger composite to create
a final page.
Typically, tiles are kept in a separate subdirectory (call it
example) to differentiate between JSP pages that are either complete in there own right or
actually use a tile layout (such as the
mainLayout used here).
Finally, we can build a full page from the pieces thus composed (except for the "body"
portion which we'll do shortly). Here is
mainMenu.jsp (the source is
<%@ page language="java" %>
<%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles" %>
<tiles:put name="title" value="Main Menu"></tiles:put>
<tiles:put name="heading"><%= session.getAttribute( "pageHeading" ) %></tiles:put>
<tiles:put name="body" value="menu.jsp"/>
tiles:insert tag causes the insertion of a particular layout at that
point in the JSP. If we had wanted to use the default values specified in the
tiles-defs.xml file, we would've simply closed this tag without providing
content. As it is, we wanted to overwrite several of the vallues to tailor this
particular instance to produce a menu.
Of the five values defined in the original layout, only three are being overridden within
this instance of the layout (the default header and footer will be used):
||The value ("Main menu") for the window title is
hard-coded and specified using the
value attribute of the
The value for the page heading is provided by providing body content for the
put tag instead of using the
value attribute. This allows
the use of dynamic JSP content (such as the value of a session variable, as here) to
specify the actual value to be passed.
This scenario is probably more realistic as hard-coded values in JSP pages do not lend
themselves to internationalization (so it is likely that the window title value would
also be dynamic).
||The value for the body page (not created yet in this
example) is hard-coded here. Remember, this value will not be treated as a string;
instead, it will be processed as the name of either another JSP page (as is the case
here) or the name of another Tiles layout (which will be resolved recursively).
|Overridden layout values in
At this stage, the only thing missing is a tile JSP named
menu.jsp. Just like the header and footer pages, this
is not going to be a complete JSP. (The source is here.)
When a user hits
mainMenu.jsp in our application, they will get a nicely
formatted and complete HTML page where all the tiles have been placed. Click here to see the generated source and here to see the page.
These pages have been kept simple deliberately. In reality, they can contain JSP logic as
well as further references to other tiles.
Note that there would have to be a session variable called
for this particular example to work. However, this can be easily changed to another
snippet of code to render this example usable.
I've made a few layout changes (indentation of inserted tiles, addition of comments where
the tiles were inserted, link format) so the resulting page can be seen as correct HTML.
However, the content is as generated.
Return to Top
Book Review of the Month
Chosen by Cindy Glass and Madhav Lakkapragada
Professional JSP Tag Libraries
by Simon Brown
This book is a great introduction and tutorial on writing JSP custom tags. It
takes a step-by-step and very detailed approach to the subject and has lots of
example code. Chapters build on the information presented in previous chapters,
so this is one of those books you really do want to read from start to finish. I
think you'll want to do this anyway...
The book can be divided, like Gaul, into three parts. The first part (Chapters
1-7) takes the reader from constructing simple tags through Body Tags and
finally to tags that cooperate with other tags to perform a task. The second
section (Chapters 8-10) discusses the situations that custom tags should be used
for and provides examples, design approaches for tags and tag libraries, and how
to validate and deploy your tags. The chapter on validation addresses validation
in the tag handler itself, in a TagExtraInfo class, and the use of the
TagLibraryValidator class to validate pages that import a tag library. The last
two chapters present a case study and brief overviews of Struts tags, Jakarta
Taglibs, and the Java Standard Tag Library (JSTL).
The book focuses on tag capabilities in JSP 1.2 but you can use most of what is
presented in JSP 1.1. In fact, the chapter on Body Tags re-visits an iteration
example from a previous chapter to demonstrate how tags that iterate over a
collection can be developed in JSP 1.1.
(John Wetherbie - Bartender, October 2002)
More info at Amazon.com
More info at Amazon.co.uk
Return to Top
Movin' them doggies on
the Cattle Drive
It's where you come to learn Java, and just like the cattle drivers
of the old west, you're expected to pull your weight along the way.
The Cattle Drive forum is where the drivers get together to complain,
uh rather, discuss their assignments and encourage each other. Thanks to the
enthusiastic initiative of Johannes de Jong, you can keep track of your progress on the drive
with the Assignment Log. If you're tough enough to get through the
nitpicking, you'll start collecting moose heads at the Cattle Drive Hall of Fame.
Gettin' them doggies...
On the Cattle Drive, about 18 Ranchers kept busy pickin' on them poor nitpickers
during September and October. Seems everybody wants to learn all about JDBC these
days as at least 8 long time Drivers were (and will be) keeping Marilyn busy
nitpickin' away on the JDBC trail.
Fresh riders on the Drive...
Got a few new Cattle Drivers signed up on the assignment log, all jumpy and
chompin' at the bit to drive them doggies. Big welcome to our latest new riders:
Barry Gaunt, Ken Cobbs, and David
Mason. The trail gets a might bit bumpy 'round these parts. Get ready...
Another moose on the wall
Yep, that's right, you make it through, you get yerself a nice
little moose to hang on the wall. Well, OK, so it's a virtual moose
on a virtual wall, but you can be right proud of 'em! Thanks to the
efforts of Marilyn deQueiroz, you can now proudly admire your
codin' accomplishments at the recently opened Cattle Drive Hall of Fame. Check it out, pardner, it's
worth a wink.
This month Ken Cobbs bagged his first moose on the Java Basics
trail of the drive. Way to go Ken! While Peter Berquist was
busy a ramblin' down the Classes and Objects trail and baggin' his second moose,
Carol Murphy and veteran Pauline McNamara
done each bagged their third moose on the Servlets trail. Congrats to all three
of you! We're all lookin' forward to hearing some darn good fish stories in
the Saloon from Peter Gragert
and Louise Haydu, who both recently hung their fourth moose
on the wall. Good job Ranchers!
Nitpicking is hard work too...
We know they're workin' reeeeeally hard, after all, we've seen what those assignments
look like once the nitpickers have combed through 'em. Hats off to Marilyn deQueiroz and Pauline McNamara for their dedication and patience with the pesky
vermin that always manage to make their way into those assignments.
Tips for the Trail...
When askin' folks a question, whether on the trail or in the Saloon, takin'
a minute to explain things to your Cardboard
Analyst and thinkin' about Asking
a Good Question is time well spent. See ya'll on the trail!
Content and format adapted from Ol' Timer Pauline McNamara's original column. -Dirk Schreckmann.
Return to Top
Hey, we've added a Tack Room. No one should be out on the range without their vital JavaRanch supplies. T-shirts, caps, lunch bags, and mugs will keep you ready whether you are out on a cattle drive or just dropping in to the saloon.
This month's special item is the JavaRanch backpack. No CS major should be caught carrying his books in anything else!
As always JavaRanch is dedicated to providing you with the best source of information on Java Programming and Engineering.
by Thomas Paul
Return to Top
Managing Editor: Carl Trusiak
Comments or suggestions for JavaRanch's NewsLetter can be sent to the NewsLetter Staff
For advertising opportunities contact NewsLetter Advertising Staff