NOTE: This is outdated information that applies only to Tapestry 3 and 4. For current information see Tapestry5RedirectException. |
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.
- Find the Tapestry service you want
- Use the getLink() method with the right parameters in the right order
- Thow a RedirectException
The following extended RedirectException makes this very easy. Thanks to Paul Ferraro for posting this gem to the mailing list.
Example (Tapestry3)
import org.apache.tapestry.IDirect; import org.apache.tapestry.IRequestCycle; import org.apache.tapestry.RedirectException; import org.apache.tapestry.Tapestry; import org.apache.tapestry.engine.ILink; import org.apache.tapestry.link.DirectLink; public class TapestryRedirectException extends RedirectException { private static final long serialVersionUID = 4049918285694842678L; public TapestryRedirectException(String url) { super(url); } public TapestryRedirectException(ILink link) { this(link.getURL()); } /** * Use this to redirect the browser to the specified page */ public TapestryRedirectException(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 TapestryRedirectException(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 TapestryRedirectException(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; } }
Example (Tapestry4)
import java.util.HashMap; import java.util.Map; import org.apache.hivemind.impl.RegistryBuilder; import org.apache.tapestry.IRequestCycle; import org.apache.tapestry.RedirectException; import org.apache.tapestry.engine.IEngineService; import org.apache.tapestry.engine.ILink; import org.apache.tapestry.services.ServiceConstants; public class TapestryRedirectException extends RedirectException { public static IEngineService getPageService() { return (IEngineService)RegistryBuilder.constructDefaultRegistry() .getService("tapestry.services.Page",IEngineService.class); } public static Map getLinkParams(Strink pageName) { Map linkParams=new HashMap(); linkParams.put(ServiceConstants.PAGE,pageName); return linkParams; } private static final long serialVersionUID = 4049918285694842678L; public TapestryRedirectException(String url) { super(url); } public TapestryRedirectException(ILink link) { this(link.getURL()); } /** * Use this to redirect the browser to the specified page */ public TapestryRedirectException(IRequestCycle cycle, String page) { this(cycle.getEngine().getInfrastructure().getLinkFactory().constructLink( TapestryRedirectException.getPageService(),false, TapestryRedirectException.getLinkParams(page),false)); } }