Action and Direct listeners

The ActionLink, DirectLink and Form components (which make use of the action and direct services) inform the application when they have been triggered using listeners.

A listener is an object that implements the IActionListener interface.

public void actionTriggered(IComponent component, IRequestCycle cycle)
  throws RequestCycleException;

Prior to release 1.0.2, it was necessary to create an object to be notified by the component; this was almost always an annonymous inner class:

public IActionListener getFormListener()
{
  return new IActionListener()
  {
    public void actionTriggered(IComponent component, IRequestCycle cycle)
      throws RequestCycleException
    {
      // perform some operation ...
    }
  };
}

Although elegant in theory, that's simply too much Java code for too little effect. Starting with Tapestry 1.0.2, it is possible to create a listener method instead.

A listener method takes the form:

public void method-name(IRequestCycle cycle)
throws RequestCycleException;

[Note]Note

The throws clause is optional and may be omitted. However, no other exception may be thrown.

In reality, listener objects have not gone away. Instead, there's a mechanism whereby a listener object is created automatically when needed. Each component includes a property, listeners, that is a collection of listener objects for the component, synthesized from the available public methods. The listener objects are properties, with the names corresponding to the method names.

[Tip]Tip

The class AbstractEngine (the base class for SimpleEngine) also implements a listeners property. This allows you to easily add listener methods to your application engine.

The earlier example is much simpler:

public void formSubmit(IRequestCycle cycle)
{
  // perform some operation ...
}

However, the property path for the listener binding must be changed, from formListener to listeners.formSubmit.