Listener methods are the main approach by which you add application-specific behavior to your application.
Listener methods are a kind of call back; they are triggered when a form is submitted or a link is clicked. The listener methods exist within your page and component classes. Components such as DirectLink and Form take a listener parameter, and you can use a listener: binding reference to use a listener method in your class as the listener. As of Tapestry 4.1.3, components that require a listener (XTile, DirectLink, InvokeListener, Suggest) no longer need an explicit listener provided for their "listener" parameter as long as a listener method exists on the component whose name is composed of the capitalized component id, prefixed by "do". For example, jwcid="clear@DirectLink" would expect a listener method called doClear() if the listener parameter is not used.
Note:The parameter type for listeners is IActionListener . Internally, Tapestry creates an object that implements that interface and uses reflection to invoke the corrsponding method on your page or component instance. On rare occasions, it is useful to create objects that implement the interface directly. For pages, components and the engine, there is a listeners property whose keys are the names of listener methods, but the listener: binding reference is easier to use.
A listener method is always a public instance method. It may take parameters, or not, and may return void or certain other types.
A listener method may return void, may return a string, or may return an object that implements IPage . The last two options are used to change the active page , the page which will render the response. Returning null will not change the active page (it defaults to the page containing the link or form components which invoked the listener method).
When using the DirectLink component, you may specify additional listener parameters . The listener parameters are encoded into the URL and will be available in a later request, when the listener is triggered.
Note:In Tapestry 3.0 and earlier, listener parameters were known as service parameters . In addition, listener methods had to be in a very fixed form, taking exactly one parameter of type IRequestCycle and returning void.
The listener can gain access these parameters in one of two ways:
getListenerParameters()
method of
IRequestCycle
<a jwcid="@DirectLink" listener="listener:doClick" parameters="ognl:{ objectId, index }"> . . . </a>
In the Java class, the listener method might look like:
public void doClick(String objectId, int index) { . . . }
Alternately, the listener method could look like:
public void doClick(IRequestCycle cycle) { Object[] parameters = cycle.getListenerParameters(); String objectId = (String)parameters[0]; int index = ((Integer)parameters[1]).intValue(); . . . }
This second case is maintained in Tapestry 4.0 mostly for backwards compatibility, or to handle the case where a single listener method must handle an indeterminate number of listener parameters.
In fact, Tapestry does a search for the most appropriate method, given the number of listener parameters:
int
and
java.lang.Integer
interchangeably).
<parameter name="listener" required="yes"/> <inject property="listenerInvoker" object="infrastructure:listenerInvoker"/>
public abstract IActionListener getListener(); public abstract ListenerInvoker getListenerInvoker(); . . . IActionListener listener = getListener(); ListenerInvoker invoker = getListenerInvoker(); invoker.invokeListener(listener, this, cycle);
It is acceptible to pass null as the listener; this saves you the necessity of checking for null when the listener is an optional parameter.