tapestry-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Apache Wiki <wikidi...@apache.org>
Subject [Tapestry Wiki] Update of "Tapestry5RedirectException" by NickWestgate
Date Thu, 09 Aug 2007 11:56:05 GMT
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

The comment on the change is:
Extended to throw page Class or page name String.

------------------------------------------------------------------------------
- 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]]
+ 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
of criticism from many people who 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:
+ Here are some examples of how to use the exception in a page or component:
  {{{
  
+     throw new RedirectException(SomePage.class);
+ 
+     // or
+ 
+     throw new RedirectException("SomePage");
+ 
+     // or
+ 
      @Inject
-     ComponentResources resources;
- ...
-     throw new RedirectException(resources.createPageLink("SomePageName", false, new Object[0]));
+     ComponentResources resources; // use this to create a Link
+     Link link = resources.createPageLink( // etc
+ 
+     throw new RedirectException(link);
  
  }}}
  
@@ -23, +32 @@

  {
      private static final long serialVersionUID = -2927007059196754228L;
  
-     protected Link link;
+     protected Link pageLink;
+     protected Class<?> pageClass;
+ 
+     public RedirectException(String pageName)
+     {
+         super(pageName);
+     }
+ 
+     public RedirectException(Class<?> pageClass)
+     {
+         this.pageClass = pageClass;
+     }
  
      public RedirectException(Link link)
      {
-         this.link = link;
+         this.pageLink = link;
      }
  
-     public Link getLink()
+     public Link getPageLink()
      {
-         return link;
+         return pageLink;
+     }
+ 
+     public Class<?> getPageClass()
+     {
+         return pageClass;
      }
  }
  
@@ -41, +66 @@

  Add this method to your !AppModule.java:
  {{{
  
-     // handle RedirectException (which contains a Link)
+     // handle RedirectException
      public static RequestExceptionHandler decorateRequestExceptionHandler(
-         final Object delegate,
-         final Response response)
+         final Object delegate, final Response response,
+         final RequestPageCache requestPageCache, final LinkFactory linkFactory,
+         final ComponentClassResolver resolver)
      {
          return new RequestExceptionHandler()
          {
              public void handleRequestException(Throwable exception) throws IOException
              {
+                 // check if wrapped
                  Throwable cause = exception;
                  if (exception.getCause() instanceof RedirectException)
+                 {
                      cause = exception.getCause();
+                 }
  
+                 // check for redirect
                  if (cause instanceof RedirectException)
                  {
+                     // check for class and string
+                     RedirectException redirect = (RedirectException)cause;
+                     Link pageLink = redirect.getPageLink();
+                     if (pageLink == null)
+                     {
+                         // handle Class (see ClassResultProcessor)
+                         String pageName = redirect.getMessage();
+                         Class<?> pageClass = redirect.getPageClass();
+                         if (pageClass != null)
+                         {
+                             pageName = resolver.resolvePageClassNameToPageName(pageClass.getName());
+                         }
+ 
+                         // handle String (see StringResultProcessor)
+                         Page page = requestPageCache.get(pageName);
+                         pageLink = linkFactory.createPageLink(page, false);
+                     }
+ 
+                     // handle Link redirect
+                     if (pageLink != null)
+                     {
-                     response.sendRedirect(((RedirectException)cause).getLink().toRedirectURI());
+                         response.sendRedirect(pageLink.toRedirectURI());
-                     return;
+                         return;
+                     }
                  }
  
+                 // no redirect so pass on the exception
                  ((RequestExceptionHandler)delegate).handleRequestException(exception);
              }
          };

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tapestry.apache.org
For additional commands, e-mail: dev-help@tapestry.apache.org


Mime
View raw message