Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Tapestry Wiki" for 
change notification.

The following page has been changed by NickWestgate:
http://wiki.apache.org/tapestry/Tapestry5RedirectException

New page:
In Tapestry 3 and 4 it was possible to throw a special kind of exception to 
redirect to another page. Howard has not implemented this in Tapestry 5 (as of 
T5.0.5) possibly because many people think (or perhaps read in a book ; - ) 
that it is misusing exceptions. Anyway, for the pragmatic ...[[BR]]

This is an example of how to use the exception in a page or component:
{{{

    @Inject
    ComponentResources resources;
...
    throw new RedirectException(resources.createPageLink("SomePageName", false, 
new Object[0]));

}}}

Here is the exception class. Put this somewhere like a utility package in your 
app:
{{{

package yourApp.utilities;

import org.apache.tapestry.Link;

public class RedirectException extends RuntimeException
{
    private static final long serialVersionUID = -2927007059196754228L;

    protected Link link;

    public RedirectException(Link link)
    {
        this.link = link;
    }

    public Link getLink()
    {
        return link;
    }
}

}}}

Add this method to your AppModule.java:
{{{

    // handle RedirectException (which contains a Link)
    public static RequestExceptionHandler decorateRequestExceptionHandler(
        final Object delegate,
        final Response response)
    {
        return new RequestExceptionHandler()
        {
            public void handleRequestException(Throwable exception) throws 
IOException
            {
                Throwable cause = exception;
                if (exception.getCause() instanceof RedirectException)
                    cause = exception.getCause();

                if (cause instanceof RedirectException)
                {
                    
response.sendRedirect(((RedirectException)cause).getLink().toRedirectURI());
                    return;
                }

                
((RequestExceptionHandler)delegate).handleRequestException(exception);
            }
        };
    }

}}}

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to