RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Rapid Java Web Application Development with Tapestry : Page 3

Tapestry is a powerful and innovative framework for developing component-based Web applications. With Tapestry 4, things get even better.


Tapestry Components

The next thing you may notice are the rather strange-looking tag attributes ("jwcid" and the like). These attributes identify Tapestry components, which are responsible for generating (or "rendering") the dynamic parts of the page (see Figure 3).

Click to enlarge 
Figure 3. Generating a Tapestry Page

Tapestry components are embedded in ordinary HTML tags, as in the following example:

<span jwcid="@Insert" value="ognl:featureDestination.price">199</span>

The jwcid attribute stands for Java Web Component ID. It identifies the dynamic component you are interested in. The component here is an Insert component, which inserts data retrieved from a property of the corresponding Java class. The value attribute indicates where the dynamic data is to be found. The ognl prefix stands for Object Graph Navigation Language, a powerful expression language that is useful for identifying properties within Java objects. In this particular case, you are fetching the price attribute nested in the featureDestination attribute of the Java class associated with this page. The contents of the tag (199) are just there for previewing purposes; it will be replaced at runtime by the dynamically generated text.

There are 50 or so built-in Tapestry components, which generally cover most Web developer needs, including loops, conditions, and date input fields. If you really need to, you can also write your own quite easily.

The Page Class

Most Tapestry pages with any significant dynamic content have a corresponding Java class, which works with the page template to fill in the dynamic parts of the page. The class should have the same name as the page, and, for convenience, extend the BasePage class (See Sidebar: Where Does Tapestry Look for Its Classes?).

A Tapestry page class typically provides the following:

  • Getters and setters for the objects used in the page
  • Methods that are called at different points in the page lifecycle
  • Methods that will be invoked by user actions on the page

The following is the class for the example Home page, which represents only the first two types of methods:

public abstract class Home extends BasePage implements PageBeginRenderListener
    public abstract DestinationOffer getFeatureDestination();
    public abstract void setFeatureDestination(DestinationOffer featureDestination);

    public void pageBeginRender(PageEvent event) {

One of the quirks of Tapestry is that page properties are often abstract, rather than being classic JavaBean properties. Another important thing to know is that, for performance reasons, Tapestry pages are pooled and can be shared between users. So you must re-initialize any user-specific data before reusing the page. By declaring abstract getters and setters for a property, you let Tapestry handle all the nitty-gritty details of property cleanup and initialization.

Tapestry also provides a number of interfaces, which you can use to implement methods that are called at certain points in the page lifecycle. One of the most useful is the PageBeginRenderListener interface, which along with the pageBeginRender() method, initialize page properties before the page is generated. Tapestry calls the pageBeginRender() method just before the page is displayed. The example initializes the featureDestination page attribute with a value retrieved from the appropriate business class.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date