NOTE: This is outdated information that applies only to Tapestry 3.

SteveGibson: This is an FAQ on tapestry-users also. The usual response is Brian Lewis' GMane posting: http://article.gmane.org/gmane.comp.java.tapestry.user/8108 I have simplified it, though the functionality will be identical under 3.0.

Note: This is pretty much meaningless under Tapestry 4.0 - see LogoutLinkTap4. From what I can see, you would probably need to use Approach 1 or approach 1 and either use an interceptor on the RestartService, or extend the RestartService. This shows why I try not to extend the "inner workings" of Tapestry or any 3rd party API - they change, sometimes inexplicably. Why AbstractEngine had the restart method is a historical question best left to those who know... ;-)

Simple Answer

Use a ServiceLink to the RestartService in your page(.html).

    <span jwcid="@ServiceLink" service="ognl:@org.apache.tapestry.Tapestry@RESTART_SERVICE">Logout</span>

This will save on writing extra classes if you don't need them, but stops you from doing other things in your listener without wrapping the Restart Service (like logging a message, giving the user the option to not logout?, etc).

Listener Approach

Firstly, you will need to define a listener method in your page class.


For newbies:

  1. Subclass org.apache.tapestry.html.BasePage

  2. Add listener method public void logout(IRequestCycle cycle) You can use any method name, just keep the signature the same. IRequestCycle is org.apache.tapestry.IRequestCycle
  3. Change page definition (.page) so that your page-specification has class set to your new class.

NB: IEngine is org.apache.tapestry.IEngine


Inside the listener method,

public void logout(IRequestCycle cycle)
{
    IEngineServiceView engine = (IEngineServiceView)getEngine();
    try {
        engine.restart(cycle);
    } catch (IOException e) {
        // do something
    }
}

If you look at the source for AbstractEngine, you will see that it invalidates the HttpSession and redirects the user to the default URL of the servlet. I personally would put any extra logic required here as it keeps it all in one place.

Thirdly add the listener to your link:

   <span jwcid="@ActionLink" listener="ognl:listeners.logout">Logout</span>

Engine Enhancement Approach

If you absolutely, positively want to enhance BaseEngine, rather than to hide this code in your BasePage enhancement...

    public void restart(IRequestCycle cycle) throws IOException
    {
        // ... Do cleanup work here / defer to a method / fire an event 
        // This needs to complete before super.restart(IRequestCycle) 
        // otherwise the rug may be pulled out from under you
        super.restart(cycle);
    }

FrequentlyAskedQuestions/LogoutLink (last edited 2011-01-02 01:58:22 by BobHarner)