Master the Tiles framework

snapshot of the page taken as our search engine crawled the Web.
The web site itself may have changed. You can check the current page or check for previous versions at the Internet Archive. Yahoo! is not affiliated with the authors of this page or responsible for its content.
Master the Tiles framework Master the Tiles framework
Presented by developerWorks, your source for great tutorials
ibm.com/developerWorks
Table of Contents
If you're viewing this document online, you can click any of the topics below to link directly to that section.
1. Getting started
..........................................................
2
2. The Tiles framework and architecture
..............................
5
3. Tile layouts
..............................................................
8
4. Tile definitions
..........................................................
18
5. Advanced Tile topics
...................................................
23
6. Lists
.......................................................................
28
7. Advanced definition concepts
........................................
34
8. Wrap-up and resources
...............................................
42
Master the Tiles framework
Page 1 of 44 Section 1. Getting started
What is this tutorial about?
This tutorial describes how to use the Tiles framework to create reusable presentation
components. (At its creation, the Tiles framework was originally called Components.
The name was changed because "components" means too many different things, but
the spirit of the original name remains.) Beyond site layouts, however, you can do
much more with tiles. You can, for example, compartmentalize your presentation tier to
better reuse layouts, HTML, and other visual components.
This tutorial strives to impart the basics of using the Tiles framework, then takes your
knowledge up a notch. By the time you finish, you will be able to use the more
advanced Tiles features to create reusable components.
Note: Throughout this tutorial, the terms tile and page are used interchangeably
because any Web resource can be a tile. A tile layout represents a special type of tile
you can use to place other tiles within it. A tile layout can be used as a tile in another
tile layout.
Getting down to specifics, this tutorial: Defines the Tiles framework and architecture. Covers the Tiles architecture and how it integrates with Struts. Clarifies key Tiles concepts. Demonstrates how to build and use a tile layout as a site template. Demonstrates how to use tile definitions both in XML and JavaServer Pages (JSP)
code. Defines tile scope and how to move objects in and out of tile scope. Works with attribute lists. Shows how to nest tiles. Demonstrates how to build and use a tile layout as a small visual component.
ibm.com/developerWorks
Presented by developerWorks, your source for great tutorials
Page 2 of 44
Master the Tiles framework
Shows how to subclass a definition. Creates a controller for a tile. Demonstrates using a tile as an
ActionForward
.
Who should take this tutorial?
If you find yourself writing the same three lines of JSP code on every page, or you want
to define complex template layouts easily, then you will benefit from this tutorial.
This tutorial assumes you have a solid understanding of Java programming, MVC
(Model-View-Controller), Model 2, and JSP technology. While a good Struts
background lets you get the most out of this tutorial, if you're adept at JSP
programming, you should be able to follow most of what is going on.
Software requirements and code installation
To complete this tutorial, you will need: A 1.1, 1.2, or 2.0 JSP-compliant servlet/JSP container.
Apache Tomcat 3.x or higher
is an excellent choice. Note: The tutorial's examples were written with a JSP
1.2-compliant container. The Tiles framework. You can get the framework as part of the
Struts 1.1
download
or standalone from the
Tiles Web site
. The source code. I've supplied two versions:
one with jar files
and
one without jar
files
for those of us who are bandwidth impaired. Struts ships with a blank war file,
struts-blank.war
(under the
webapps
directory), which illustrates which
configuration files and jar files you need, and where you typically put them. You'll use
the same structure for the example code.
See
Resources
on page 43 for information on these materials and additional resources.
About the author
Presented by developerWorks, your source for great tutorials
ibm.com/developerWorks
Master the Tiles framework
Page 3 of 44 Rick Hightower
enjoys working with Java technology, Ant, Struts, the IBM Emerging
Technologies Toolkit (ETTK), and XDoclet. Rick currently serves as the CTO of
ArcMind Inc.
, a mentoring, consulting, and training company focusing on enterprise
development. Rick, a regular IBM developerWorks contributor, has written more than
10 tutorials ranging from EJB (Enterprise JavaBeans) technology to Web services to
XDoclet.
While working at eBlox, Rick and the eBlox team used Struts to build two frameworks
and an ASP (application service provider) for online e-commerce stores. They started
using Struts long before the 1.0 release. Rick recently helped put together a
well-received course for Trivera Technologies on Struts that runs on Tomcat, Resin EE,
WebSphere Studio Application Developer, and others.
Rick co-authored Mastering Struts, 2nd edition with James Goodwill (Wrox Press). Rick
also co-wrote
Java Tools for Extreme Programming
(John
Wiley & Sons, 2001), the
best-selling software development book on Amazon.com for three months in 2002. It
covers applying Ant, JUnit, Cactus, and more to J2EE (Java 2 Platform, Enterprise
Edition) development. Rick also contributed two chapters to Mastering Tomcat
Development (John Wiley & Sons, 2002), as well as many other publications.
Rick spoke at the 2003 JavaOne Developers Conference on EJB CMP/CMR and
XDoclet, and at TheServerSide.com Software Symposium on J2EE development with
XDoclet. Additionally, Rick has spoken at JDJEdge and WebServicesEdge. Moreover,
Rick spoke about advanced Struts topics at the Complete Programmer Network
software symposiums (in six different cities across the US).
When not traveling around the country teaching the Trivera Struts course, speaking at
conferences about Struts, or doing Struts consulting, Rick enjoys drinking coffee at an
all-night coffee shop, writing about Struts and other topics, and writing about himself in
the third person.
ibm.com/developerWorks
Presented by developerWorks, your source for great tutorials
Page 4 of 44
Master the Tiles framework Section 2. The Tiles framework and architecture
Tiles framework
The Tiles framework turns the
jsp:includes
concept inside out -- thus letting you
more feasibly create reusable pages. With the Tiles framework, developers can build
pages by assembling reusable tiles. You should think of tiles as visual components.
A tile layout is a special JSP page that allows tiles to be placed. Tile layouts dictate
where the tiles will be laid out on the page. In many respects the tile layout resembles a
template layout. In fact, if you have used Struts templates before, then you will note
that the Tile frameworks is backwards compatible with the template custom tag library.
Clarification of terms
The terms presented in this tutorial may seem overwhelming at first, so before we
discuss the Tiles framework in more detail, let's define some important terms.
Glossary of terms
Tiles The template framework for Struts to create reusable presentation components.
Page A Web resource included by a tile layout.
Tile The same as a page.
Region An area in a tile layout that inserts another tile. Regions have logical names like
header, footer, and so on.
Tile layout A JSP page that describes where other pages should be positioned. Acting
as a template, a tile layout defines regions where other tiles are inserted. A tile layout
can be a tile to another tile layout.
Definition Defines parameters for calling a tile layout.
Tile layouts
In some ways, the tile layout works like a display function. To use a tile layout, call it
with the
tiles:insert
tag. When you invoke the tile layout, you pass parameters.
Presented by developerWorks, your source for great tutorials
ibm.com/developerWorks
Master the Tiles framework
Page 5 of 44 The parameters become attributes to the tile layout; for example, the parameters are
put into tile scope.
The parameters passed when invoking a tile can be other JSP pages or Web
resources, which you can insert at predefined locations, called regions, in the layout.
The parameters also consist of strings you can insert into the tile layout. In fact, you
can pass many types of objects as parameters to the tile. The parameters become
attributes in the tile scope available only to that tile.
The tile scope resembles a page scope in that the tile scope is less general than a
request scope. The tile scope lets tile users pass arguments, called attributes, to the
tile. The tile scope lets you pass variables available only to that tile layout or tile.
Special custom tags let you copy attributes from tile scope to page, request, session, or
application scope, or display the