Pass Parameters with a PageRedirectException

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Pass Parameters with a PageRedirectException

Patrick Casey
       
        Hi Folks,

        I'm trying to implement the POST->Redirect->GET model inside of my
Tapestry app. I've done it before in most things I've written so the users
don't get those nasty scary "This Page contains POST data" when they push
refresh after a post. In this case though I'm trying to do it within
Tapestry (without driving a knife through the framework and just grabbing
ahold of the HTTPServeletRequest and redirecting there), and I'm a bit
stumped.

        I'm at a point in my code where I know:

        1) I need to redirect to a page named "genericlist".
        2) I need to pass said page a set of parameters (so it knows what to
list).

        I can redirect with a simple:

        Throw new PageRedirectException("genericList");

        But that doesn't let me pass along a set of parameters so that the
target page knows what to do.

        Is there a way I can pass a parameter array along with a
PageRedirectException?

        --- Pat



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

Reply | Threaded
Open this post in threaded view
|

RE: Pass Parameters with a PageRedirectException

richard.hensley
Patrick,

Try using a RedirectException. A PageRedirectException just does a
cycle.activatePage().

To formulate a correct url, use the appropriate engine service, such a Page.

Richard

-----Original Message-----
From: Patrick Casey [mailto:[hidden email]]
Sent: Friday, May 13, 2005 10:32 AM
To: 'Tapestry users'
Subject: Pass Parameters with a PageRedirectException

       
        Hi Folks,

        I'm trying to implement the POST->Redirect->GET model inside of my
Tapestry app. I've done it before in most things I've written so the users
don't get those nasty scary "This Page contains POST data" when they push
refresh after a post. In this case though I'm trying to do it within
Tapestry (without driving a knife through the framework and just grabbing
ahold of the HTTPServeletRequest and redirecting there), and I'm a bit
stumped.

        I'm at a point in my code where I know:

        1) I need to redirect to a page named "genericlist".
        2) I need to pass said page a set of parameters (so it knows what to
list).

        I can redirect with a simple:

        Throw new PageRedirectException("genericList");

        But that doesn't let me pass along a set of parameters so that the
target page knows what to do.

        Is there a way I can pass a parameter array along with a
PageRedirectException?

        --- Pat



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


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

Reply | Threaded
Open this post in threaded view
|

RE: Pass Parameters with a PageRedirectException

richard.hensley
In reply to this post by Patrick Casey
To answer your real question, yes a service has the ability to generate a
URL with parameters.

-----Original Message-----
From: Patrick Casey [mailto:[hidden email]]
Sent: Friday, May 13, 2005 10:32 AM
To: 'Tapestry users'
Subject: Pass Parameters with a PageRedirectException

       
        Hi Folks,

        I'm trying to implement the POST->Redirect->GET model inside of my
Tapestry app. I've done it before in most things I've written so the users
don't get those nasty scary "This Page contains POST data" when they push
refresh after a post. In this case though I'm trying to do it within
Tapestry (without driving a knife through the framework and just grabbing
ahold of the HTTPServeletRequest and redirecting there), and I'm a bit
stumped.

        I'm at a point in my code where I know:

        1) I need to redirect to a page named "genericlist".
        2) I need to pass said page a set of parameters (so it knows what to
list).

        I can redirect with a simple:

        Throw new PageRedirectException("genericList");

        But that doesn't let me pass along a set of parameters so that the
target page knows what to do.

        Is there a way I can pass a parameter array along with a
PageRedirectException?

        --- Pat



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


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

Reply | Threaded
Open this post in threaded view
|

RE: Pass Parameters with a PageRedirectException

richard.hensley
In reply to this post by Patrick Casey
Patrick,

And some more thoughts... :)

Actually, you might want to use the External service to accomplish what you
want. The following is a short example of how to generate a proper URL and
throw the exception.

        IEngineService svc =
getEngine().getService(Tapestry.EXTERNAL_SERVICE);
        throw new RedirectException(svc.getLink(cycle, page, new
Object[]{p1,p2}).getURL());

For this to work, your Page needs to implement the IExternalPage interface.
The URL above will cause a method called     public void
activateExternalPage(Object[] parameters, IRequestCycle cycle) to be invoked
on your page.

If you use the direct service, look at how the DirectLink component uses the
DirectService.getLink() method to get a feel for how to write code that
generates a proper URL.

The components that might be of interest are DirectLink, PageLink,
ExternalLink, or ServiceLink.

-----Original Message-----
From: Hensley, Richard [mailto:[hidden email]]
Sent: Friday, May 13, 2005 10:54 AM
To: Tapestry users
Subject: RE: Pass Parameters with a PageRedirectException

To answer your real question, yes a service has the ability to generate a
URL with parameters.

-----Original Message-----
From: Patrick Casey [mailto:[hidden email]]
Sent: Friday, May 13, 2005 10:32 AM
To: 'Tapestry users'
Subject: Pass Parameters with a PageRedirectException

       
        Hi Folks,

        I'm trying to implement the POST->Redirect->GET model inside of my
Tapestry app. I've done it before in most things I've written so the users
don't get those nasty scary "This Page contains POST data" when they push
refresh after a post. In this case though I'm trying to do it within
Tapestry (without driving a knife through the framework and just grabbing
ahold of the HTTPServeletRequest and redirecting there), and I'm a bit
stumped.

        I'm at a point in my code where I know:

        1) I need to redirect to a page named "genericlist".
        2) I need to pass said page a set of parameters (so it knows what to
list).

        I can redirect with a simple:

        Throw new PageRedirectException("genericList");

        But that doesn't let me pass along a set of parameters so that the
target page knows what to do.

        Is there a way I can pass a parameter array along with a
PageRedirectException?

        --- Pat



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


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


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

Reply | Threaded
Open this post in threaded view
|

RE: Pass Parameters with a PageRedirectException

Patrick Casey
In reply to this post by richard.hensley

        I'm trying to make that work, but I'm having problems with a part of
Tapestry I thought I understood. Basically I'm having trouble generating a
valid url :(. Perhaps you can offer a hint on what I'm doing wrong here.

Object[] parms = ... some parameters
String url = cycle.getEngine().getService(Tapestry.DIRECT_SERVICE)
                                        .getLink(cycle, this,
parms).getURL();

This does, in fact, generate a URL, but the URL looks like

/Corinna/app?service=direct/1/WorkOrder/null&sp=...

Note the fact that the component field is null?!? So, naturally, when
Tapestry tries to handle the above link it looks for a component named
"null" on the WorkOrder page, fails to find it, and throws an exception.

I don't want a component to handle this request though; I want to page
itself to handle it. How do I set up a link back to the IPage instance
instead of a contained component?

What (I think) the url should look like is:

/Corinna/app?service=direct/1/WorkOrder/$DirectLink&sp=...

I can't seem to find a syntax that makes getLink() spit out the proper link
though.

Any suggestions?

        --- Pat



> -----Original Message-----
> From: Hensley, Richard [mailto:[hidden email]]
> Sent: Friday, May 13, 2005 10:53 AM
> To: Tapestry users
> Subject: RE: Pass Parameters with a PageRedirectException
>
> Patrick,
>
> Try using a RedirectException. A PageRedirectException just does a
> cycle.activatePage().
>
> To formulate a correct url, use the appropriate engine service, such a
> Page.
>
> Richard
>
> -----Original Message-----
> From: Patrick Casey [mailto:[hidden email]]
> Sent: Friday, May 13, 2005 10:32 AM
> To: 'Tapestry users'
> Subject: Pass Parameters with a PageRedirectException
>
>
> Hi Folks,
>
> I'm trying to implement the POST->Redirect->GET model inside of my
> Tapestry app. I've done it before in most things I've written so the users
> don't get those nasty scary "This Page contains POST data" when they push
> refresh after a post. In this case though I'm trying to do it within
> Tapestry (without driving a knife through the framework and just grabbing
> ahold of the HTTPServeletRequest and redirecting there), and I'm a bit
> stumped.
>
> I'm at a point in my code where I know:
>
> 1) I need to redirect to a page named "genericlist".
> 2) I need to pass said page a set of parameters (so it knows what to
> list).
>
> I can redirect with a simple:
>
> Throw new PageRedirectException("genericList");
>
> But that doesn't let me pass along a set of parameters so that the
> target page knows what to do.
>
> Is there a way I can pass a parameter array along with a
> PageRedirectException?
>
> --- Pat
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]



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

Reply | Threaded
Open this post in threaded view
|

Re: Pass Parameters with a PageRedirectException

pferraro
In reply to this post by Patrick Casey
To implement POST->Redirect->GET, you need to use RedirectException, NOT
PageRedirectException.
Throwing PageRedirectException simply interrupts the current render
cycle and activates the specified page within the same request.
To generate the url required for RedirectException you will need to use
the appropriate engine service.

Because it the code required to generate urls for RedirectException
requires so much tedious logic, I've created a simple extension to the
RedirectException to ease the construction of urls for the Page,
External, and Direct services (see below).  I posted this code well over
a year ago, but at the time there was some disagreement among the
developers (I was not yet one) regarding its utility.  Now that I am
one, I'll see that 4.0 addresses the current shortcomings of
RedirectException.  I would also love to rename PageRedirectException at
some point so that it ceases to become a point of confusion...

Paul


public class RedirectException extends org.apache.tapestry.RedirectException
{
    private static final long serialVersionUID = 4049918285694842678L;

    public RedirectException(String url)
    {
        super(url);
    }
   
    public RedirectException(ILink link)
    {
        this(link.getURL());
    }
   
    public RedirectException(IRequestCycle cycle, String page)
    {
       
this(cycle.getEngine().getService(Tapestry.PAGE_SERVICE).getLink(cycle,
cycle.getPage(), new String[] { page }));
    }
 
    public RedirectException(IRequestCycle cycle, String page, Object
parameters)
    {
       
this(cycle.getEngine().getService(Tapestry.EXTERNAL_SERVICE).getLink(cycle,
cycle.getPage(), getServiceParameters(page, parameters)));
    }
 
    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;
    }
}

Patrick Casey wrote:

>
> Hi Folks,
>
> I'm trying to implement the POST->Redirect->GET model inside of my
>Tapestry app. I've done it before in most things I've written so the users
>don't get those nasty scary "This Page contains POST data" when they push
>refresh after a post. In this case though I'm trying to do it within
>Tapestry (without driving a knife through the framework and just grabbing
>ahold of the HTTPServeletRequest and redirecting there), and I'm a bit
>stumped.
>
> I'm at a point in my code where I know:
>
> 1) I need to redirect to a page named "genericlist".
> 2) I need to pass said page a set of parameters (so it knows what to
>list).
>
> I can redirect with a simple:
>
> Throw new PageRedirectException("genericList");
>
> But that doesn't let me pass along a set of parameters so that the
>target page knows what to do.
>
> Is there a way I can pass a parameter array along with a
>PageRedirectException?
>
> --- Pat
>
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: [hidden email]
>For additional commands, e-mail: [hidden email]
>
>  
>


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

Reply | Threaded
Open this post in threaded view
|

RE: Pass Parameters with a PageRedirectException

richard.hensley
In reply to this post by Patrick Casey
Paul,

This wonderful nugget is now immortalized in Tapestry lore on the Tapestry
wiki at

http://wiki.apache.org/jakarta-tapestry/EasyBrowserRedirection

Richard

-----Original Message-----
From: Paul Ferraro [mailto:[hidden email]]
Sent: Friday, May 13, 2005 11:18 AM
To: Tapestry users
Subject: Re: Pass Parameters with a PageRedirectException

To implement POST->Redirect->GET, you need to use RedirectException, NOT
PageRedirectException.
Throwing PageRedirectException simply interrupts the current render
cycle and activates the specified page within the same request.
To generate the url required for RedirectException you will need to use
the appropriate engine service.

Because it the code required to generate urls for RedirectException
requires so much tedious logic, I've created a simple extension to the
RedirectException to ease the construction of urls for the Page,
External, and Direct services (see below).  I posted this code well over
a year ago, but at the time there was some disagreement among the
developers (I was not yet one) regarding its utility.  Now that I am
one, I'll see that 4.0 addresses the current shortcomings of
RedirectException.  I would also love to rename PageRedirectException at
some point so that it ceases to become a point of confusion...

Paul


public class RedirectException extends org.apache.tapestry.RedirectException
{
    private static final long serialVersionUID = 4049918285694842678L;

    public RedirectException(String url)
    {
        super(url);
    }
   
    public RedirectException(ILink link)
    {
        this(link.getURL());
    }
   
    public RedirectException(IRequestCycle cycle, String page)
    {
       
this(cycle.getEngine().getService(Tapestry.PAGE_SERVICE).getLink(cycle,
cycle.getPage(), new String[] { page }));
    }
 
    public RedirectException(IRequestCycle cycle, String page, Object
parameters)
    {
       
this(cycle.getEngine().getService(Tapestry.EXTERNAL_SERVICE).getLink(cycle,
cycle.getPage(), getServiceParameters(page, parameters)));
    }
 
    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;
    }
}

Patrick Casey wrote:

>
> Hi Folks,
>
> I'm trying to implement the POST->Redirect->GET model inside of my
>Tapestry app. I've done it before in most things I've written so the users
>don't get those nasty scary "This Page contains POST data" when they push
>refresh after a post. In this case though I'm trying to do it within
>Tapestry (without driving a knife through the framework and just grabbing
>ahold of the HTTPServeletRequest and redirecting there), and I'm a bit
>stumped.
>
> I'm at a point in my code where I know:
>
> 1) I need to redirect to a page named "genericlist".
> 2) I need to pass said page a set of parameters (so it knows what to
>list).
>
> I can redirect with a simple:
>
> Throw new PageRedirectException("genericList");
>
> But that doesn't let me pass along a set of parameters so that the
>target page knows what to do.
>
> Is there a way I can pass a parameter array along with a
>PageRedirectException?
>
> --- Pat
>
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: [hidden email]
>For additional commands, e-mail: [hidden email]
>
>  
>


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


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