Adding Context to Links in Tapestry

Introduction

Tapestry looks at the URL and assumes that anything after the page name is the context that is being passed to the page. So a URL like:

http://www.site.com/MyPage/5

Is going to expect that the number 5 is the context needed on MyPage.  You can have multiple contexts passed to a page like this:

http://www.site.com/MyPage/5/foo

This will pass two contexts to the page.  Depending on what the page is expecting to receive in the context, these values may be uses as they are or they might be coerced into something else.  For example, the five might represent the ID of some object that will be retrieved from the Hibernate.

In the case of a single context value being passed, MyPage can get the value into a page property by doing something like this:

@PageActivationContext
@Property
private int myNumber;

The PageActivationContext annotation tells Tapestry to put the value on the URL that comes after the page into that variable. With Hibernate involved you can do something like this:

@PageActivationContext
@Property
private Person aPerson;

Tapestry will tell Hibernate to get the person object with an id of 5 and assign it to the variable aPerson.

If the page has multiple context values being passed, they are handled through the onActivate method:

public void onActivate(int myNumber, String myString) {
//set appropriate page properties
}

PageLink

Thats a little background. The main thing I wanted to look at in this post is how to get links that pass in these context values in the first place.

This snippet shows a pagelink that will give us something of the form:
http://www.site.com/MyPage/5
Assuming that the person.id is 5.

<t:pagelink page="MyPage" context="person.id">${person.name}</t:pagelink>

If we need to pass multiple context values it can be done like this.

<t:pagelink page="MyPage" context="[person.id,order.id]">view order</t:pagelink>

If person.id is 5 and order.id is 22 this link will produce:
http://www.site.com/MyPage/5/22

Returning Page and Context from a Method

Tapestry lets us return values in Java that specify the next page to load. This is commonly seen in the onSuccess method which can look like this:

    public Object onSuccess() {
       //Do what needs to be done
       return "MyPage";
    }

At first it looks like we can simply append the values like:

    public Object onSuccess() {
       //Do what needs to be done
       return "MyPage/" + person.id + "/" + order.id;
    }

But this does not work because Tapestry can’t find the page “MyPage/5/22”. It only knows about “MyPage”. To add a context you will need to inject the pageRenderLinkSource and use it to build the context.

    @Inject
    private PageRenderLinkSource pageRenderLS;

    public Object onSuccess() {
       //Do what needs to be done
       return pageRenderLS.createPageRenderLinkWithContext("MyPage", person.id);
    }

You can add additional context values to the parameters as well.

    @Inject
    private PageRenderLinkSource pageRenderLS;

    public Object onSuccess() {
       //Do what needs to be done
       return pageRenderLS.createPageRenderLinkWithContext("MyPage", person.id, order.id);
    }

About 

2 Replies to “Adding Context to Links in Tapestry”

  1. Hi
    thanks for the course. How can i call a page doing the same things above, except that i want the parameter person.id to disappear when i call MyPage.
    I dont wanna see on my URI : https://……./MyPage/5 but i only wanna see https://……/MyPage
    thanks

    1. If you want the parameter to disappear after called, I think you are going to need to take the person.id, stick it in the session, and then redirect to a page without the ID in the URL. But then I wonder why put it in the URL in the first place. You can just stick it in the session before you load the page.

Leave a Reply to Simon Cancel reply

Your email address will not be published.