Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: fixed language param of code macro

...

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
xml
languagexml

<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
java
languagejava

  @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
java
languagejava

  @OnEvent(value = "action", component = "select")
  void valueChosen(int value)
  {
    this.value = value;
  }

...

The previous example may be rewritten as:

Code Block
java
languagejava

  void onActionFromSelect(int value)
  {
    this.value = value;
  }

...

In other words, there's no need to do this:

Code Block
java
languagejava

  void onActionFromRunQuery()
  {
    try
    {
      dao.executeQuery();
    }
    catch (JDBCException ex)
    {
      throw new RuntimeException(ex);
    }
  }

Instead, you may simply say:

Code Block
java
languagejava

  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
java
languagejava

  Object onException(Throwable cause)
  {
    message = cause.getMessage();

    return this;
  }

...