[Jakarta-tapestry Wiki] Update of "EasyBrowserRedirection" by RichardHensley

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[Jakarta-tapestry Wiki] Update of "EasyBrowserRedirection" by RichardHensley

Apache Wiki
Dear Wiki user,

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

The following page has been changed by RichardHensley:
http://wiki.apache.org/jakarta-tapestry/EasyBrowserRedirection

New page:
##language:en
== Easy Browser Redirection ==
A common problem is the need to redirect the browser from a Tapestry Listener. The way this is accomplished is with the following steps.

 1. Find the Tapestry service you want
 1. Use the getLink() method with the right parameters in the right order
 1. Thow a RedirectException

The following extended RedirectException makes this very easy. Thanks to Paul Ferraro for posting this gem to the mailing list.
=== Example ===
{{{
public class TapestryRedirectException extends RedirectException
{
    private static final long serialVersionUID = 4049918285694842678L;

    public RedirectException(String url)
    {
        super(url);
    }
   
    public RedirectException(ILink link)
    {
        this(link.getURL());
    }
   
    /**
     * Use this to redirect the browser to the specified page
     */
    public RedirectException(IRequestCycle cycle, String page)
    {
        this(cycle.getEngine().getService(Tapestry.PAGE_SERVICE).getLink(cycle,
          cycle.getPage(), new String[] { page }));
    }
 
    /**
     * Use this to redirect the browser to the specified page that implements the IExternalPage
     * interface
     */
    public RedirectException(IRequestCycle cycle, String page, Object
        parameters)
    {
         this(cycle.getEngine().getService(Tapestry.EXTERNAL_SERVICE).getLink(cycle,
             cycle.getPage(), getServiceParameters(page, parameters)));
    }
 
    /**
     * Use this to redirect the browser to the specified listenener on the component
     * that implements the IDirect interface.
     */    
    public RedirectException(IRequestCycle cycle, IDirect direct, Object
        parameters)
    {
        this(cycle.getEngine().getService(Tapestry.DIRECT_SERVICE).getLink(cycle,
          direct, DirectLink.constructServiceParameters(parameters)));
    }

    private static Object[] getServiceParameters(String targetPage,
        Object parameter)
    {
        Object[] pageParameters = DirectLink.constructServiceParameters(parameter);
        if (pageParameters == null)
        {  
            return new Object[] { targetPage };
        }
        Object[] parameters = new Object[pageParameters.length + 1];
        parameters[0] = targetPage;
        System.arraycopy(pageParameters, 0, parameters, 1, pageParameters.length);
        return parameters;
    }
}
}}}

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]