Tapestry Training -- From The Source

Let me help you get your team up to speed in Tapestry ... fast. Visit howardlewisship.com for details on training, mentoring and support!

Sunday, June 19, 2005

Listener method improvement

Had an idea recently; another improvement for Tapestry listener methods. Listener methods return void, and if a method wants to activate another page, it must accept the IRequestCycle as a parameter, so it can invoke activate() on it.

What if listener methods could also return a string (the name of a page), or a page instance to activate? Especially paired with injection of pages (and better yet, an annotation), this results in something very flexible and even familiar to Struts and WebWork users.

Just blogging while Eclipse warms up ... should have this written, tested and checked in shortly.

Ok, so its all done, came out great: Tapestry 3.0.x:

public void doShowDetails(IRequestCycle cycle)
{
  Object[] parameters = cycle.getServiceParameters();
  Long accountIdLong = (Long)parameters[0];
  long accountId = accountIdLong.longValue();

  Details page = (Details)cycle.getPage("Details");

  page.setAccountId(accountId);

  cycle.activate(page);
}
Tapestry 4.0 alpha-3:
public void doShowDetails(IRequestCycle cycle, long accountId)
{
  Details page = (Details)cycle.getPage("Details");
  page.setAccountId(accountId);

  cycle.activate(page);
}
Tapestry 4.0 beta-1:
@InjectPage("Details")
public abstract Details getDetailsPage();

public IPage doShowDetails(long accountId)
{
  Details details = getDetailsPage();
  details.setAccountId(accountId);

  return details;
}

4 comments:

Mike said...

I admit I'm not an expert on Tapestry (still learning) but it would seem that if you don't need to set any properties on a page before you activate it you could just activate it yourself. If you do have to set properties on the page then you would normally activate it after you set the properties or you'd go ahead and just give the IRequestCycle instance to the page to activate itself. I'm not sure how changing the return value is such an advantage, especially considering that it would be either String or IPage which means that you'd be returning Object from the method which makes it kind of ambiguous?

Now, a way to do redirects without needing to throw an exception is what I'd love to see. :)

-Mike
http://www.mikebosch.com

Mike said...

I posted before your examples went up. I hadn't been up to speed on what you were doing in 4.0 so I have to say that I definitely like the way the listener methods are going with parameters being set directly in the listener method invocation. Definitely makes things nicer from my point of view.

-Mike

Dmitro Rashko said...

It would be great to have
independant configuration of
site map including error pages.

James Carman said...

Cool, Howard! I like it!