Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: discuss validation

...

Code Block
java
java
  @OnEvent(component = "select")
  void valueChosen(int value)
  {
    this.value = value;F
  }

Tapestry does two things here:

  • Because of the annotation, it identifies method valueChosen() as the method to invoke.
  • When the link is clicked, it converts the context value from a string to an integer and passes it into the method.
Since
since5.3

Starting in release 5.3, Tapestry will validate that the component, if any, identified for the event handler method actually exists in the containing component's template. This helps with typos in annotations (or in the naming conventions identified below).

In the above example, the valueChosen() method will be invoked when the default event, "action", occurs in the select component (and has at least one context value).

...

Code Block
java
java
  void onActionFromSelect(int value)
  {
    this.value = value;
  }
Info

Note from Howard: I've found that I prefer the naming convention approach, and reserve the annotation just for situations that don't otherwise fit.

Method Return Values

Main Article: Page Navigation

...

When an event handler method is invoked, the strings are converted back into values, or even objects. A ValueEncoder is used to convert between client-side strings and server-side objects. The ValueEncoderSource service provides the necessary value encoders.

As shown in the example above, most of the parameters passed to the event handler method are derived from the values provided in the event context. Each successive method parameter matches against a value provided in the event context (the context parameter of the ActionLink component; though many components have a similar context parameter).

In some cases, it is desirable to have direct access to the context (for example, to adapt to cases where there are a variable number of context values). The context values may be passed to an event handler method as parameter of the following types:

The latter two should be avoided, they may be removed in a future release. In all of these cases, the context parameter acts as a freebie; it doesn't match against a context value as it represents all context values.

Accessing Request Query Parameters

A parameter may be annotated with the @RequestParameter annotation; this allows a query parameter to be extracted from the request, converted to the correct type, and passed to the method. Again, this doesn't count against the event context values.

Method Matching

An event handler method will only be invoked if the context contains at least as many values as the method has parameters. Methods with too many parameters will be silently skipped.

Collections

Tapestry will silently skip over a method if there are insufficient values in the context to satisfy the number of parameters requested.

EventContext parameters, and parameters annotated with @RequestParameter, do not count against this limit.

Method Ordering

When multiple methods match within the same class, Tapestry will invoke them in ascending alphabetical order. When there are multiple overrides of the same method name, Tapestry invokes them in descending order by number of parameters. In general, these situations don't happen ... in most cases, only a single method is required to handle a specific event form a specific component.

An event handler method may return the value true to indicate that the event has been handled; this immediately stops the search for additional methods in the same class (or in base classes) or in containing componentsTo designate that an event handler method should be invoked regardless of how many context parameters are available, change the method to accept a single parameter of type Object[], type List, or type EventContext.

Event Bubbling

The event will bubble up the hierarchy, until it is aborted. The event is aborted when an event handler method returns a non-null value.

...