The Example Application
The best way to get a feel of Tapestry is to work through some examples. In this tutorial, you will work on a Web site designed to sell discount plane tickets for various destinations. The application uses a simple business model (see Figure 1
- Promotional offers are represented by the
DestinationOffer class, which contains the city of destination and the price.
FeatureDestinations interface provides business methods concerning a set of daily promotional offers.
FeatureDestinationsImpl class is a memory-based implementation of this interface.
|Figure 1. UML Model for the Example Application|
Your First Tapestry Page
Let's start off with a simple page. Every Tapestry application has a home page called (appropriately enough) Home. Your home page will display a randomly chosen feature destination. It also will provide a link back to the same page in order to display another offer. The Home page requires two main things:
- A page template, containing a mixture of HTML code and dynamic Tapestry components (more about components shortly)
- A corresponding Java class, which provides data for the dynamic parts of the page
In Tapestry 3, each page also needed a page specification file. This file is an XML file describing the mapping between the page template and the Java class. Although page specifications can be useful, and even necessary in more complex pages, Tapestry 4 uses Java 5 annotations and extra tag attributes to greatly reduce the need to write one for each and every page. You won't need to worry about them for the examples in this article.
The Page Template
Let's start by looking at the page template:
<title>Tutorial: Introduction to Tapestry</title>
<h3>Online Travel Discounts</h3>
<h4>One of today's feature destinations:</h4>
A trip to
<span jwcid="@Insert" value="ognl:featureDestination.destination">Paris</span>
for only $<span jwcid="@Insert" value="ognl:featureDestination.price">199</span>
The first thing you may notice is that the template is very close to normal HTML. In fact, you can safely display it in a Web browser (see Figure 2). Because it uses Normal HTML tags instead of JSP, JSTL, or JSF tags, a non-Java-savvy Webmaster can easily build and maintain the page templates using ordinary Web design tools. JSF pages, on the other hand, use tags that are quite alien to a traditional HTML Web designer, and that cannot be previewed in a Web browser. Java developers also generally find it more convenient to be able to preview pages without having to deploy to an application server.