...
Let's review a simple example. Here's a portion of the template for a page (let's call it "Chooser") that lets the user choose a number between 1 and 10:
Code Block | ||
---|---|---|
| ||
<p> Choose a number from 1 to 10:
<t:count end="10" value="index">
<a t:id="select" t:type="actionlink" context="index">${index}</t:comp>
</t:count>
</p>
|
...
When a component event occurs, Tapestry invokes any event handler methods that you have identified for that event. You can identify your event handler methods via a naming convention (see Method Naming Convention below), or via the @OnEvent annotation.
Code Block | ||
---|---|---|
| ||
@OnEvent(component = "select")
void valueChosen(int value)
{
this.value = value;
}
|
...
For some components, more than one type of event can occur, in which case you will want to be more specific:
Code Block | ||
---|---|---|
| ||
@OnEvent(value = "action", component = "select")
void valueChosen(int value)
{
this.value = value;
}
|
...
The previous example may be rewritten as:
Code Block | ||
---|---|---|
| ||
void onActionFromSelect(int value)
{
this.value = value;
}
|
...
In other words, there's no need to do this:
Code Block | ||
---|---|---|
| ||
void onActionFromRunQuery()
{
try
{
dao.executeQuery();
}
catch (JDBCException ex)
{
throw new RuntimeException(ex);
}
}
|
Instead, you may simply say:
Code Block | ||
---|---|---|
| ||
void onActionFromRunQuery() throws JDBCException
{
dao.executeQuery();
}
|
...
Tapestry emits a new event, of type "exception", passing the thrown exception as the context. In fact, the exception is wrapped inside a ComponentEventException, from which you may extract the event type and context.
Thus:
Code Block | ||
---|---|---|
| ||
Object onException(Throwable cause)
{
message = cause.getMessage();
return this;
}
|
...