Thursday, April 8, 2010

Passing paramters: activateExternalPage

Tapestry 5 doesn't seem to distinguish between a regular page and an external page. There is no BasePage class anymore and there are no special page interfaces like IExternal page.

Makes sense given the new architecture. It is simpler. Everything is easier than it used to be, it is just taking me longer than I expected to figure that out. I have a few more entries to make, but this one just came up and it's a quick one.

Every page has an "onActivate" function that you can override. Actually there are a lot of functions you can override, during each step in the process. Knowing exactly when these functions are called is essential to making your application interface work.

In Tapestry 3 you would have to declare your page something like this:

public abstract class Item extends BasePage implements IExternalPage, PageRenderListener {

Also in Tapestry 3 you'd have to implement "activateExtrenalPage" in order to
access the parsed query string:

public void activateExternalPage(Object[] params, IRequestCycle cycle) {

Then inside this function you can store values from the params array into class properties for later use. The query strings are pretty cryptic.

Now in Tapestry 5 it is just a straight up class:

public class Item {

You also need to inject your request identifier (giving you an interface to the usual HttpServletRequest data:

@Inject private Request _request;

Then add an onActivate function like this:
void onActivate() {
if (_request.getParameter("itemn") != null) {
_itemn = _request.getParameter("itemn");
}
}


Note that you can access parameters by readable string name. The query string parameters can then be in any order. Note that if the parameter is null that usually means it is not in the query string. You can do other things in this function, like plug in a default value, but keep in mind that onActivate gets called every time a page is either requested or a form submitted.

For more on these functions you can override take a look at the output on this page:
http://jumpstart.doublenegative.com.au:8080/jumpstart/examples/navigation/whatiscalledandwhen

These you implement by simply declaring a function named the same as these steps.

Also the Render phase can be broken down even more using annotations that represent the flow described in the chart on this page:

http://tapestry.apache.org/tapestry5/guide/rendering.html

The annotations available at this time are:
@SetupRender
@BeginRender
@BeforeRenderTemplate
@RenderTemplate
@BeforeRenderBody
@RenderBody
@AfterRenderBody
@AfterRenderTemplate
@AfterRender
@CleanupRender

Usage for example would look something like this:

@SetupRender void JimsToDoB4Render() {
ItemData itemData = new ItemData(_itemn);
_itemDescription = itemData.getDescription();
_itemPrice = itemData.getPrice();
}

No comments:

Post a Comment