You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Antonio Petrelli <an...@gmail.com> on 2009/02/26 22:41:50 UTC

[tools-2.0] How to access Writer in tool

Hi all
Is there a way to access the writer that is used to write in the
response, from a request-scoped tool?
I tried using the HttpServletResponse's writer, but it writes included
resources in wrong order (as I suspected). It is the same effect that
you would have in JSP tags when you use HttpServletResponse's writer
instead of PageContext's one.
I took a look at TilesTool and I noticed that, in fact, the included
resources are transformed in strings and then returned to the
template. Sincerely I don't like it.
In TilesViewServlet I noticed that, if "bufferOutput" is true, a
StringWriter is used, otherwise the HttpServletResponse's one is used.
I wish to use the correct writer in both cases.

Thanks in advance
Antonio

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


Re: [tools-2.0] How to access Writer in tool

Posted by Antonio Petrelli <an...@gmail.com>.
2009/2/27 Antonio Petrelli <an...@gmail.com>:
> 2009/2/26 Nathan Bubna <nb...@gmail.com>:
>> On Thu, Feb 26, 2009 at 1:41 PM, Antonio Petrelli
>> <an...@gmail.com> wrote:
>>> Hi all
>>> Is there a way to access the writer that is used to write in the
>>> response, from a request-scoped tool?
>>
>> In Velocity 1.6, we introduced the
>> org.apache.velocity.runtime.Renderable interface, which has a "boolean
>> execute(context, writer)" method.  If, when rendering a reference,
>> Velocity comes across a Renderable, it calls that method instead of
>> calling toString() and writing that value.  but even if you do
>> implement Renderable, please consider having your toString() be
>> functional (e.g. passing a StringWriter and null context to
>> execute(...)).
>
> Thanks a lot, I will try the Renderable interface.

Tried and it worked :-)
Instead of executing the rendering code directly in the called method,
I create an instance of an anonymous Renderable that is executed when
needed.
Just if you want to take a look:
http://svn.eu.apache.org/viewvc/tiles/sandbox/trunk/tiles-velocity/src/main/java/org/apache/tiles/velocity/template/InsertDefinitionVModel.java?view=markup

See the "execute" method.

Thanks again!
Antonio

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


Re: [tools-2.0] How to access Writer in tool

Posted by Antonio Petrelli <an...@gmail.com>.
2009/2/26 Nathan Bubna <nb...@gmail.com>:
> On Thu, Feb 26, 2009 at 1:41 PM, Antonio Petrelli
> <an...@gmail.com> wrote:
>> Hi all
>> Is there a way to access the writer that is used to write in the
>> response, from a request-scoped tool?
>
> In Velocity 1.6, we introduced the
> org.apache.velocity.runtime.Renderable interface, which has a "boolean
> execute(context, writer)" method.  If, when rendering a reference,
> Velocity comes across a Renderable, it calls that method instead of
> calling toString() and writing that value.  but even if you do
> implement Renderable, please consider having your toString() be
> functional (e.g. passing a StringWriter and null context to
> execute(...)).

Thanks a lot, I will try the Renderable interface.

Antonio

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


Re: [tools-2.0] How to access Writer in tool

Posted by Nathan Bubna <nb...@gmail.com>.
On Thu, Feb 26, 2009 at 1:41 PM, Antonio Petrelli
<an...@gmail.com> wrote:
> Hi all
> Is there a way to access the writer that is used to write in the
> response, from a request-scoped tool?

In Velocity 1.6, we introduced the
org.apache.velocity.runtime.Renderable interface, which has a "boolean
execute(context, writer)" method.  If, when rendering a reference,
Velocity comes across a Renderable, it calls that method instead of
calling toString() and writing that value.  but even if you do
implement Renderable, please consider having your toString() be
functional (e.g. passing a StringWriter and null context to
execute(...)).

> I tried using the HttpServletResponse's writer, but it writes included
> resources in wrong order (as I suspected). It is the same effect that
> you would have in JSP tags when you use HttpServletResponse's writer
> instead of PageContext's one.

yeah, we want content to run through Velocity, direct writing is frowned upon.

> I took a look at TilesTool and I noticed that, in fact, the included
> resources are transformed in strings and then returned to the
> template. Sincerely I don't like it.

it keeps things orderly and flexible, allowing, for instance, tools to
grab each other out of the context and interact with the same API.
but yes, it can be a performance hit in situations involving a lot of
content.

> In TilesViewServlet I noticed that, if "bufferOutput" is true, a
> StringWriter is used, otherwise the HttpServletResponse's one is used.
> I wish to use the correct writer in both cases.

please do!  but your options are restricted to Renderable (Velocity
1.6+) or toString() (any version).  Velocity is not in the business of
handing out the writer to objects before it is time to render them,
sorry.  Users can hack that in easily by extending VelocityView and
overwriting performMerge(..) to drop the writer in the context, but
that won't work well for a TilesTool.

> Thanks in advance
> Antonio
>
> ---------------------------------------------------------------------
> 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