GETting Tapestry

I’m trying to wrap my head around Tapestry’s contexts and event handling. The details are hidden in various sections of the documentation so it’s been a lot of trial and error. My goal is to be able to handle URL parameters in a way I can understand and process with some intelligence.

On a link, you can pass in a context:

<t:pagelink page=”games/Edit” context=”game”>${game.name}</t:pagelink>

In this instance, I’m passing a Hibernate entity to the game edit page. The URL will be http://www.atomicgamer.com/game/edit/5 where 5 is the entity id. On the edit page, we have several ways to handle get this value.

Note:  Since we passed the Hibernate entity and not just the id, Tapestry is smart enough to fetch the entity from the session. That’s why the following examples use variables of Game, not Long.

PageActivationContext Annotation

We can use the @PageActivationContext annotation and game will be automatically populated.

@Property
@PageActivationContext
private Game game;

onActivate Convention

If we defined a method onActivate(Game game), the parameter will be populated and we can assign it to an instance variable.

OnEvent Annotation

If we don’t want to use the onActivate convention, we can use the @OnEvent annotation on a method instead.

@OnEvent(“activate”)
public void annotationTriggered(Game game)…

While all of the above examples will get you access to the context, you will want to watch for the order of operations if you try to mix them.

I wanted to use PageActivationContext to automatically populate my instance variable. Then, I wanted to use the OnEvent method to check for a errors (the instance variable will be null if it doesn’t exist in the database).

However, the OnEvent method is fired before the PageActivateContext. The instance variable will be null regardless. I tried onActivate() (without any parameters) and while that is fired after the PageActivateContext, I felt that was getting too far into happenstance for me to feel happy about the solution.

The current order of operation appears to be:

  1. @OnEvent(“activate”)
  2. @PageActivationContext
  3. onActivate()

I think I’ll just stick with one of these instead of trying to mix them in any fashion.

I’m still learning Tapestry 5 and this is just one of the many growing pains I’m suffering at the moment. It’s not that the framework can’t do what I want it to do. Rather, I’m not sure which way I want it done that makes sense down the road. I’m lacking clarity in the big picture of a Tapestry application.

Leave a Comment