Engine Services

Engine services provide the structure for building a web application from individual pages and components.

Each engine service has a unique name. Well known names exist for the basic services (page, action, direct, etc., described in a later section).

Engine services are responsible for creating URLs (which are inserted into the response HTML) and for later responding to those same URLs. This keeps the meaning of URLs localized. In a typical servlet or JSP application, code in one place creates the URL for some servlet to interpret. The servlet is in a completely different section of code. In situations where the servlet's behavior is extended, it may be necessary to change the structure of the URL the servlet processes ... and this requires finding every location such a URL is constructed and fixing it. This is the kind of inflexible, ad-hoc, buggy solution Tapestry is designed to eliminate.

Most services have a relationship to a particular component. The basic services (action, direct, page) each have a corresponding component (ActionLink, DirectLink, PageLink). The following example shows how the PageLink component is used to create a link between application pages.

First, an extract from the page's HTML template:

Click <a jwcid="login">here</a> to login.

This is combined with the a <component> declaration in the the page's specification:

<component id="login" type="PageLink">
  <static-binding name="page">Login</static-binding>
</component>

The login component will locate the page service, and provide 'Login' (the name of the target page) as a parameter. The page service will build and return an appropriate URL, which the login component will incorporate into the <a> hyperlink it generates.

The resulting HTML:

Click <a href="/servlet-path?service=page&context=Login">here</a> to login.

If the user later clicks that link, the application will invoke the page service to handle the URL; it will extract the page name (Login) and render that page.

The other services are more or less complicated, but share the same basic trait: the service provides the URL and later responds if the URL is triggered.

Links (ActionLink, DirectLink, etc.) and Forms use services in slightly different ways. Links encode all the information directly into the URL whereas Forms encode most of the information as hidden form fields.

Figure 5.2. Services and Gestures

Services and Gestures

In the first part, a service generates a Gesture and then extracts the full URL from it, for use as the href attribute of the <a> tag.

In the second part, a service is used to access the servlet path (which becomes the action attribute of the <form> element). The query parameters are individually extracted and encoded as hidden fields in the form.