You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@turbine.apache.org by Kevin Rutherford <ke...@planetcad.com> on 2002/02/07 19:20:15 UTC

Redirecting from an action (was RE: TemplateScreen.doRedirect())

I have another issue with 302 redirects. We use Turbine 2.1 with JSP rather
than Velocity, but I think the issue is the same either way.

We need to perform a 302 redirect from within our action code (I will spare
you the reasons unless someone really wants to know why). I started digging
into the Turbine code and found the place in Turbine.java where it does the
redirect (calls the HttpResponse.doRedirect() method). However, this only
occurs AFTER the page class is completely done building the page.

DefaultPage.java is the class that ends up calling the action class and then
calling either the layout or the screen (depending on whether the layout is
null) to build the page output. In my case, it does not make sense for
DefaultPage to continue building the page if the action has already told
Turbine to do a redirect.

Does it make sense to check (in DefaultPage.java) for a redirect AFTER
invoking the action, and then skip the screen rendering step if a redirect
has been requested? When I modified DefaultPage.java to do this, it seemed
to fix the problem, but I want to make sure that this is appropriate. If so,
I can submit a patch to DefaultPage.java. Or is there some other "correct"
way to accomplish a 302 redirect from within an action class?

Kevin

> -----Original Message-----
> From: Darren Gilroy [mailto:DGilroy@CONSONUS.com]
> Sent: Thursday, February 07, 2002 9:23 AM
> To: 'Turbine Users List'
> Subject: RE: TemplateScreen.doRedirect()
> 
> 
> I solved this with
> 
> RunData.declareDirectResponse()
> RunData.getResponse().sendRedirect()
> 
> FYI & Thanks
> -best-darr-
> 
>  -----Original Message-----
> From: 	Darren Gilroy [mailto:DGilroy@CONSONUS.com] 
> Sent:	Thursday, February 07, 2002 7:35 AM
> To:	'Turbine Users List'
> Subject:	RE: TemplateScreen.doRedirect()
> 
> Gary - 
> 
> Hey, I'm having trouble with RunData.setRedirectURI()  -- I'm calling
> RunData.setStatusCode() as directed, and I am getting a 302 
> response, but
> not with the URL that I specified in setRedirectURI.  I get
> http://localhost:8180/app/servlet/app/tempalte/file.vm/x/2/red
> irected/true?j
> sessionid= ...
> 
> I am calling the setRedirectURI from a screen class that is 
> extended from
> VelocitySecureScreen, in the doBuildTemplate() method.  Is 
> that not a legal
> place to call this method?  It is necessary to move it to an action or
> something?
> 
> Thanks!
> -best-darr-
> 
>  -----Original Message-----
> From: 	Gary Bisaga [mailto:garybisaga@maximus.com] 
> Sent:	Wednesday, February 06, 2002 3:19 PM
> To:	Turbine Users List
> Subject:	RE: TemplateScreen.doRedirect()
> 
> Dazza- found the answer. Look at TemplatePage - the 
> doBuildAfterAction uses
> TurbineTemplate converts the template name to a screen name 
> if no screen
> name is given. But is this really what you want to do?
> 
> Seems like though, upon further reflection, you might want to 
> start the page
> all over again (e.g. to give you a chance to change the 
> layout etc.) and so
> to do a real redirect (data.setRedirectURI). What's more, 
> usually in these
> kinds of situations I've found that a real redirect is safer 
> because the
> browser's URL line is now correct: instead of showing the old,
> redirected-from URL, it will show the new, redirected-to one.
> 
> <>< gary
> 
> -----Original Message-----
> From: Darren Gilroy [mailto:DGilroy@CONSONUS.com]
> Sent: Wednesday, February 06, 2002 4:22 PM
> To: Turbine Users List
> Subject: TemplateScreen.doRedirect()
> 
> 
> Hi -
> 
> I'm calling doRedirect() from my screen class, and It's not 
> working how I
> expected.  I hope somebody can adjust my expectations.
> 
> I expected doRedirect( data, templateName ) to load and run 
> the screen class
> that it would run if you called the template with
> /app/servlet/app/template/something.vm
> 
> I see a doRedirect( data, screenName, templateName ) - How 
> can I look up the
> screenName to use based on the templateName, using the same logic that
> Turbine normally does?
> 
> Or, do you recommend a data.setRedirectURI() instead? Or 
> something else?
> 
> Thanks!
> -best-darr-
> 
> 
> --
> To unsubscribe, e-mail:
> <ma...@jakarta.apache.org>
> For additional commands, e-mail:
> <ma...@jakarta.apache.org>
> 
> 
> --
> To unsubscribe, e-mail:
> <ma...@jakarta.apache.org>
> For additional commands, e-mail:
> <ma...@jakarta.apache.org>
> 
> --
> To unsubscribe, e-mail:
> <ma...@jakarta.apache.org>
> For additional commands, e-mail:
> <ma...@jakarta.apache.org>
> 
> --
> To unsubscribe, e-mail:   
> <ma...@jakarta.apache.org>
> For additional commands, e-mail: 
> <ma...@jakarta.apache.org>
> 

Re: Redirecting from an action (was RE: TemplateScreen.doRedirect())

Posted by Brian Lawler <bl...@branuity.com>.
Kevin-

We want to accomplish the same thing in our application at branuity.  The way that we accomplish the redirect is to create a velocity template that does nothing but set up the redirect, and set up a corresponding layout that does nothing but include the $screen_placeholder so that the other navigation stuff isn't rendered either.  So here is the way it looks in our code:

=========================================================
Action class (we are using the referer to figure out where to redirect to):

 ...

    private void redirect(RunData data, Context context)
    {
        // Upon successful completion, set the template to the dummy screen
        // and redirect the browser to the actual requested URL.
        StringBuffer redirect = data.getRequest().getRequestURL();
        if(data.getRequest().getQueryString() != null)
            redirect.append("?").append(data.getRequest().getQueryString());
        context.put("redirectURI", redirect.toString());
        data.setScreenTemplate("Redirect.vm");
    }

 ...

=========================================================
screens/Redirect.vm (NOTE: these are the ONLY 2 LINES in this template!!)

    $data.setStatusCode(302)
    $data.setRedirectURI($redirectURI) 

=========================================================
layouts/Redirect.vm (NOTE: only 1 line in this template!)

$screen_placeholder


The end result here is that the browser receives nothing but a redirect to the URI specified in the action, and since we changed the template from inside the action, none of the unnecessary screen rendering takes place, which I believe is your desired behavior.

HTH

-Brian


On Thu, 07 Feb 2002 11:20:15 -0700
Kevin Rutherford <ke...@planetcad.com> wrote:

> I have another issue with 302 redirects. We use Turbine 2.1 with JSP rather
> than Velocity, but I think the issue is the same either way.
> 
> We need to perform a 302 redirect from within our action code (I will spare
> you the reasons unless someone really wants to know why). I started digging
> into the Turbine code and found the place in Turbine.java where it does the
> redirect (calls the HttpResponse.doRedirect() method). However, this only
> occurs AFTER the page class is completely done building the page.
> 
> DefaultPage.java is the class that ends up calling the action class and then
> calling either the layout or the screen (depending on whether the layout is
> null) to build the page output. In my case, it does not make sense for
> DefaultPage to continue building the page if the action has already told
> Turbine to do a redirect.
> 
> Does it make sense to check (in DefaultPage.java) for a redirect AFTER
> invoking the action, and then skip the screen rendering step if a redirect
> has been requested? When I modified DefaultPage.java to do this, it seemed
> to fix the problem, but I want to make sure that this is appropriate. If so,
> I can submit a patch to DefaultPage.java. Or is there some other "correct"
> way to accomplish a 302 redirect from within an action class?
> 
> Kevin
> 
> > -----Original Message-----
> > From: Darren Gilroy [mailto:DGilroy@CONSONUS.com]
> > Sent: Thursday, February 07, 2002 9:23 AM
> > To: 'Turbine Users List'
> > Subject: RE: TemplateScreen.doRedirect()
> > 
> > 
> > I solved this with
> > 
> > RunData.declareDirectResponse()
> > RunData.getResponse().sendRedirect()
> > 
> > FYI & Thanks
> > -best-darr-
> > 
> >  -----Original Message-----
> > From: 	Darren Gilroy [mailto:DGilroy@CONSONUS.com] 
> > Sent:	Thursday, February 07, 2002 7:35 AM
> > To:	'Turbine Users List'
> > Subject:	RE: TemplateScreen.doRedirect()
> > 
> > Gary - 
> > 
> > Hey, I'm having trouble with RunData.setRedirectURI()  -- I'm calling
> > RunData.setStatusCode() as directed, and I am getting a 302 
> > response, but
> > not with the URL that I specified in setRedirectURI.  I get
> > http://localhost:8180/app/servlet/app/tempalte/file.vm/x/2/red
> > irected/true?j
> > sessionid= ...
> > 
> > I am calling the setRedirectURI from a screen class that is 
> > extended from
> > VelocitySecureScreen, in the doBuildTemplate() method.  Is 
> > that not a legal
> > place to call this method?  It is necessary to move it to an action or
> > something?
> > 
> > Thanks!
> > -best-darr-
> > 
> >  -----Original Message-----
> > From: 	Gary Bisaga [mailto:garybisaga@maximus.com] 
> > Sent:	Wednesday, February 06, 2002 3:19 PM
> > To:	Turbine Users List
> > Subject:	RE: TemplateScreen.doRedirect()
> > 
> > Dazza- found the answer. Look at TemplatePage - the 
> > doBuildAfterAction uses
> > TurbineTemplate converts the template name to a screen name 
> > if no screen
> > name is given. But is this really what you want to do?
> > 
> > Seems like though, upon further reflection, you might want to 
> > start the page
> > all over again (e.g. to give you a chance to change the 
> > layout etc.) and so
> > to do a real redirect (data.setRedirectURI). What's more, 
> > usually in these
> > kinds of situations I've found that a real redirect is safer 
> > because the
> > browser's URL line is now correct: instead of showing the old,
> > redirected-from URL, it will show the new, redirected-to one.
> > 
> > <>< gary
> > 
> > -----Original Message-----
> > From: Darren Gilroy [mailto:DGilroy@CONSONUS.com]
> > Sent: Wednesday, February 06, 2002 4:22 PM
> > To: Turbine Users List
> > Subject: TemplateScreen.doRedirect()
> > 
> > 
> > Hi -
> > 
> > I'm calling doRedirect() from my screen class, and It's not 
> > working how I
> > expected.  I hope somebody can adjust my expectations.
> > 
> > I expected doRedirect( data, templateName ) to load and run 
> > the screen class
> > that it would run if you called the template with
> > /app/servlet/app/template/something.vm
> > 
> > I see a doRedirect( data, screenName, templateName ) - How 
> > can I look up the
> > screenName to use based on the templateName, using the same logic that
> > Turbine normally does?
> > 
> > Or, do you recommend a data.setRedirectURI() instead? Or 
> > something else?
> > 
> > Thanks!
> > -best-darr-
> > 
> > 
> > --
> > To unsubscribe, e-mail:
> > <ma...@jakarta.apache.org>
> > For additional commands, e-mail:
> > <ma...@jakarta.apache.org>
> > 
> > 
> > --
> > To unsubscribe, e-mail:
> > <ma...@jakarta.apache.org>
> > For additional commands, e-mail:
> > <ma...@jakarta.apache.org>
> > 
> > --
> > To unsubscribe, e-mail:
> > <ma...@jakarta.apache.org>
> > For additional commands, e-mail:
> > <ma...@jakarta.apache.org>
> > 
> > --
> > To unsubscribe, e-mail:   
> > <ma...@jakarta.apache.org>
> > For additional commands, e-mail: 
> > <ma...@jakarta.apache.org>
> > 
> 


-- 
brian lawler
branuity
617 front           | v: 415.217.5052
san francisco 94111 | m: 415.307.5277
brian@branuity.com  | f: 415.217.5060



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>