You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by "White, Tim" <Ti...@qwest.com> on 2008/06/25 00:13:32 UTC

Server-side redirect from VelocityTools?

Hi -
 
 I'm pretty sure that since the response is already set by the time that
a .vm template is running via VelocityViewServlet, that you can't do a
server-side redirect at that point.
 
 Is that true, or is there a way to forward to a different page directly
from logic in a .vm w/o JavaScript?
 
 For example:
 
   #if ($myVar == null)
        #redirect to some other page
   #end
 
Thanks,
 
Tim


This communication is the property of Qwest and may contain confidential or
privileged information. Unauthorized use of this communication is strictly 
prohibited and may be unlawful.  If you have received this communication 
in error, please immediately notify the sender by reply e-mail and destroy 
all copies of the communication and any attachments.

Re: Server-side redirect from VelocityTools?

Posted by Claude Brisson <cl...@renegat.net>.
Le jeudi 26 juin 2008 à 19:21 -0700, Nathan Bubna a écrit :

> which is essentially using Velocity to script your "action
> controller".  though there are "real" scripting languages better
> suited to this, i suppose it at least reduces the number of
> technologies/languages you have to think about. :)

Yes, althought I'd like to give a try to JRuby or Jython one day.

But VTL also makes very easy displaying debug screens!


  Claude



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org


Re: Server-side redirect from VelocityTools?

Posted by Nathan Bubna <nb...@gmail.com>.
On Thu, Jun 26, 2008 at 5:37 PM, Claude Brisson <cl...@renegat.net> wrote:
> Le jeudi 26 juin 2008 à 15:42 -0600, White, Tim a écrit :
>> > I don't use any centralized controller anymore - never understood why
>> it was an
>> > advantage. So I'm using a kind of MV paradigm with a decentralized
>> controller,
>> > and I'm very happy with it. It's quite straightforward and more
>> flexible.
>>
>> Right - I'm trying to do more of a 'pull' MVC model, where I centralize
>> the business logic into Velocity Tools, rather than into the
>> controllers.
>>
>> A classic example of needing to do a server side redirect though is "are
>> we authenticated?  No?  Back to the login page, please".
>
> Ah. For this very specific example, I use a filter (the Velosurf's
> AuthenticationFilter) - but for a tiny webapp, it could hold.

yeah, filters are a great fit for authentication.

> I'll give another example:
>
> Whenever I commit forms (which have many and/or text fields), I use the
> "redirect after post" technic, mainly to avoid the "would you like to
> resubmit data?" (or alike) dialog which pops up when the user just wants
> to refresh the page.
>
> For this purpose, I use small "action templates" (*.do => my
> ActionServlet) in which I use VTL as a basic scripting language (who can
> do the most can do the least) that look typically like this:
>
> #if($some_model_object.validate($params))
>  #do_something_with($params)
>  #redirect("/thank_you.vtl")
> #else
>  $logger.error("Houston, we've got a problem: $query")
>  #seterror("please try again!")
>  #redirect("/the_form.vtl")
> #end

which is essentially using Velocity to script your "action
controller".  though there are "real" scripting languages better
suited to this, i suppose it at least reduces the number of
technologies/languages you have to think about. :)

>
>
>        Claude
>
>> Tim
>>
>>
>> This communication is the property of Qwest and may contain confidential or
>> privileged information. Unauthorized use of this communication is strictly
>> prohibited and may be unlawful.  If you have received this communication
>> in error, please immediately notify the sender by reply e-mail and destroy
>> all copies of the communication and any attachments.
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
>> For additional commands, e-mail: user-help@velocity.apache.org
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org


RE: Server-side redirect from VelocityTools?

Posted by Claude Brisson <cl...@renegat.net>.
Le jeudi 26 juin 2008 à 15:42 -0600, White, Tim a écrit :
> > I don't use any centralized controller anymore - never understood why
> it was an 
> > advantage. So I'm using a kind of MV paradigm with a decentralized
> controller, 
> > and I'm very happy with it. It's quite straightforward and more
> flexible.
> 
> Right - I'm trying to do more of a 'pull' MVC model, where I centralize
> the business logic into Velocity Tools, rather than into the
> controllers.
> 
> A classic example of needing to do a server side redirect though is "are
> we authenticated?  No?  Back to the login page, please".

Ah. For this very specific example, I use a filter (the Velosurf's
AuthenticationFilter) - but for a tiny webapp, it could hold.

I'll give another example:

Whenever I commit forms (which have many and/or text fields), I use the
"redirect after post" technic, mainly to avoid the "would you like to
resubmit data?" (or alike) dialog which pops up when the user just wants
to refresh the page.

For this purpose, I use small "action templates" (*.do => my
ActionServlet) in which I use VTL as a basic scripting language (who can
do the most can do the least) that look typically like this:

#if($some_model_object.validate($params))
  #do_something_with($params)
  #redirect("/thank_you.vtl")
#else
  $logger.error("Houston, we've got a problem: $query")
  #seterror("please try again!")
  #redirect("/the_form.vtl")
#end



	Claude

> Tim
> 
> 
> This communication is the property of Qwest and may contain confidential or
> privileged information. Unauthorized use of this communication is strictly 
> prohibited and may be unlawful.  If you have received this communication 
> in error, please immediately notify the sender by reply e-mail and destroy 
> all copies of the communication and any attachments.
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org


RE: Server-side redirect from VelocityTools?

Posted by "White, Tim" <Ti...@qwest.com>.
> I don't use any centralized controller anymore - never understood why
it was an 
> advantage. So I'm using a kind of MV paradigm with a decentralized
controller, 
> and I'm very happy with it. It's quite straightforward and more
flexible.

Right - I'm trying to do more of a 'pull' MVC model, where I centralize
the business logic into Velocity Tools, rather than into the
controllers.

A classic example of needing to do a server side redirect though is "are
we authenticated?  No?  Back to the login page, please".

Tim


This communication is the property of Qwest and may contain confidential or
privileged information. Unauthorized use of this communication is strictly 
prohibited and may be unlawful.  If you have received this communication 
in error, please immediately notify the sender by reply e-mail and destroy 
all copies of the communication and any attachments.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org


Re: Server-side redirect from VelocityTools?

Posted by Claude Brisson <cl...@renegat.net>.
Le mercredi 25 juin 2008 à 13:08 -0400, Christopher Schultz a écrit :
> Claude,
> 
> Claude Brisson wrote:
> > My strategy about redirects is to map the templates that need them to an
> > ActionServlet that overload VVS.mergeTemplate to use a string buffer.
> 
> Why not simply redirect before the view is evaluated? I don't really 
> understand the use case, here. Shouldn't redirect logic be in the 
> controller, not the view?

I don't use any centralized controller anymore - never understood why it
was an advantage. So I'm using a kind of MV paradigm with a
decentralized controller, and I'm very happy with it. It's quite
straightforward and more flexible.


  Claude

> -chris
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org


Re: Server-side redirect from VelocityTools?

Posted by Christopher Schultz <ch...@christopherschultz.net>.
Claude,

Claude Brisson wrote:
> My strategy about redirects is to map the templates that need them to an
> ActionServlet that overload VVS.mergeTemplate to use a string buffer.

Why not simply redirect before the view is evaluated? I don't really 
understand the use case, here. Shouldn't redirect logic be in the 
controller, not the view?

-chris



Re: Server-side redirect from VelocityTools?

Posted by Claude Brisson <cl...@renegat.net>.
Hi.

My strategy about redirects is to map the templates that need them to an
ActionServlet that overload VVS.mergeTemplate to use a string buffer.

I then define some macros :

#macro(forward $url)
  $request.session.servletContext.getRequestDispatcher($url).forward($request,$response)
#end

#macro(redirect $url)
  $response.sendRedirect($url)
#end



  Claude

Le mardi 24 juin 2008 à 16:13 -0600, White, Tim a écrit :
> Hi -
>  
>  I'm pretty sure that since the response is already set by the time that
> a .vm template is running via VelocityViewServlet, that you can't do a
> server-side redirect at that point.
>  
>  Is that true, or is there a way to forward to a different page directly
> from logic in a .vm w/o JavaScript?
>  
>  For example:
>  
>    #if ($myVar == null)
>         #redirect to some other page
>    #end
>  
> Thanks,
>  
> Tim
> 
> 
> This communication is the property of Qwest and may contain confidential or
> privileged information. Unauthorized use of this communication is strictly 
> prohibited and may be unlawful.  If you have received this communication 
> in error, please immediately notify the sender by reply e-mail and destroy 
> all copies of the communication and any attachments.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org


Re: Server-side redirect from VelocityTools?

Posted by Nathan Bubna <nb...@gmail.com>.
On Wed, Jun 25, 2008 at 7:11 AM, Christopher Schultz
<ch...@christopherschultz.net> wrote:
> Nathan,
>
> Nathan Bubna wrote:
>>
>> On Tue, Jun 24, 2008 at 3:13 PM, White, Tim <Ti...@qwest.com> wrote:
>>>
>>> Hi -
>>>
>>>  I'm pretty sure that since the response is already set by the time that
>>> a .vm template is running via VelocityViewServlet, that you can't do a
>>> server-side redirect at that point.
>>
>> True, unless you are using VelocityTools 2 and set:
>>
>>   org.apache.velocity.tools.bufferOutput
>>
>> to true in your servlet's init-params.
>
> Is it true? I think you get a default buffer for output from the
> ServletOutputStream in the first place, so there's some wiggle-room.

that wouldn't be something i'd rely on.

> I often set the Content-Type in my VMs.
>
> Hmm... but I am using VelocityLayoutServlet, which I think builds the whole
> response in memory before streaming it. That could definitely be a factor.

yeah, in VLS, the requested template is fully buffered so it can be
inserted into the layout template.   this means you can redirect from
the requested template, but not from the layout template.

> -chris
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org


Re: Server-side redirect from VelocityTools?

Posted by Christopher Schultz <ch...@christopherschultz.net>.
Nathan,

Nathan Bubna wrote:
> On Tue, Jun 24, 2008 at 3:13 PM, White, Tim <Ti...@qwest.com> wrote:
>> Hi -
>>
>>  I'm pretty sure that since the response is already set by the time that
>> a .vm template is running via VelocityViewServlet, that you can't do a
>> server-side redirect at that point.
> 
> True, unless you are using VelocityTools 2 and set:
> 
>    org.apache.velocity.tools.bufferOutput
> 
> to true in your servlet's init-params.

Is it true? I think you get a default buffer for output from the 
ServletOutputStream in the first place, so there's some wiggle-room.

I often set the Content-Type in my VMs.

Hmm... but I am using VelocityLayoutServlet, which I think builds the 
whole response in memory before streaming it. That could definitely be a 
factor.

-chris


Re: Server-side redirect from VelocityTools?

Posted by Nathan Bubna <nb...@gmail.com>.
On Tue, Jun 24, 2008 at 3:13 PM, White, Tim <Ti...@qwest.com> wrote:
> Hi -
>
>  I'm pretty sure that since the response is already set by the time that
> a .vm template is running via VelocityViewServlet, that you can't do a
> server-side redirect at that point.

True, unless you are using VelocityTools 2 and set:

   org.apache.velocity.tools.bufferOutput

to true in your servlet's init-params.

>  Is that true, or is there a way to forward to a different page directly
> from logic in a .vm w/o JavaScript?

not unless you do the above, in which case $response.sendRedirect(url)
should do the trick.

>
>  For example:
>
>   #if ($myVar == null)
>        #redirect to some other page
>   #end
>
> Thanks,
>
> Tim
>
>
> This communication is the property of Qwest and may contain confidential or
> privileged information. Unauthorized use of this communication is strictly
> prohibited and may be unlawful.  If you have received this communication
> in error, please immediately notify the sender by reply e-mail and destroy
> all copies of the communication and any attachments.
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org