Menu

Dedicated to Financial Engineering

Tapestry - Part I: Hello Tapestry

Introduction

Tapestry 5 is framework for creating web applications using Java. The framework allows you to generate HTML pages using Java code. This a good thing if you have complex business logic that takes data from the user, database, online resources to present a result as a webpage. We wanted to do something like that for one of our econometric models so we decided to give Tapestry a go for building our web application. Along the way we learned quite a lot about the framework. Some of that we want to share here in a series of blogs. The complete series should be enough to build simple GUI for your Java business logic.

In this blog we will create a hello-world style example using Tapestry 5. The goal of this example is to get you up and running with Tapestry using the skeleton setup that is included with Tapestry. Based on this initial setup we will make changes to get a hello-world page.

Prerequisites

This tutorial uses Eclipse with Maven

Setup

The Apache Tapestry online documentation shows you how to setup a skeleton application. Please follow the steps explained here to get the skeleton project up and running using the provided Maven archetype.

Once you have completed the setup you should get something like this.

Note that the first time you try to run the Maven project it will go and download Tapestry and all its dependencies. This will likely take sometime. So don’t worry if the first time takes long. After the first run you’ll have all the required files in your local Maven repository so all the next runs will be fast.

Creating Hello World Page

To keep things simple our application is only going to be a single page. By convention any Tapestry page consists of a generic layout part and page-specific part. You could do without the layout and create only pages, but that would break the Tapestry standard setup, so we'll not do that here. The layout is defined in layout.java and layout.tml. For example, the navigation bar in the skeleton project is created in the layout as it is used on all pages. To remove the bar, it must be removed from the tml file. Try to replace your layout.tml file with the following code.

If you reload the page in you browser the navigation bar will be gone. The layout is now very minimal. It just defines some meta data for the pages and adds the footer.

Let’s remove the pages we don’t need. Remove

About.java

About.tml

Contact.java

Contact.tml

Login.java

Login.tml

You will get an error in the index file, because there is a reference to the non-existing class About. Fix this by simply removing the member variable About and the method onActionFromLearnMore(). We now have a one-page application (two if you count the 404 error page). Finally, we clean up the index page by removing all the Ajax references and layout from index.tml. We are left with:

Notice that the first line of the index page calls the general layout tml. If you remove "t:type="layout" the reference will be dropped and only layout of index.tml will be used. Alternatively a different tml could be referenced, for example"t:type="layout_admin_section". This makes the rendering of pages very flexibel.

The code above will give the following page.

Now we are ready to implement the classic hello world. We first create the input field. For this we use the form mix-in. This component can be setup in the tml file by adding the code

This code pretty much speaks for itself and gives us a html form with input like so

Once we have the name we need to post it to the page. To do this we use the “if” component. If there is a result to post, we render a block of text with hello username, otherwise we do nothing. This can be done in the tml using

Once the form is setup we need to implement the logic for the page. We need to have a text field object to link to the text field in the tml file and a string variable to store the text field value. (As we have only one form we can have it auto-generated by Tapestry. No configuration is required in this case.) In the tml files we created an if-statement based on “result”. This variable should also be in the java class so Tapestry can find it.

Onpassivate sets the output form to page and posts it to the next page by appending parameters to the url. The on activate reads the parameters as the page is being rendered. Before rendering the page we check if a name have been entered so we know if the result should be rendered or only the input box. That’s it! You have just setup a hello world!

Topics

What We Do

Modeling

We build financial models. We do this both within the Ugly Duckling team and embedded in your organization. When building models we often use the existing Ugly Duckling code base and research. This makes the modeling faster and more efficient.

Validation

We love details. When given the chance, we deep dive into a model and validate its mathematical, financial and implementation details. We also find it a rich experience to explore the models of others and learn for their approach. In most cases we contribute to make an even better model thanks to the feedback we provide as part of the process.

Research

Ugly Duckling is dedicated to expand the body of mathematical finance knowledge. To do so we invest in a continuous research effort. These efforts keep us up-to-dated on the new developments in finance. So when you encounter a challenge for which no solution exists in the literature, please call us. We can help.