You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by bluejoe <bl...@sdb.cnic.cn> on 2008/09/11 08:39:17 UTC

question on Velocity: How to let Velocity keep silent when some exceptions occur?

  Hi, I am a user of Velocity, now I have a question: How to let Velocity keep silent when some exceptions occur?
       
  For example, I wrote codes:
       
    my name is: $o.foo()
       
  In some cases, foo() may throw some exceptions, like NullException, etc. It seems that Velocity will throw these exceptions too. But I want Velocity to regard the statement as:
       
    my name is: <empty>
       
  Is there any way to let Velocity ignore the errors? 
       
Sincerely Yours,
bluejoe
2008-9-11

Re: question on Velocity: How to let Velocity keep silent when some exceptions occur?

Posted by Raymond Auge <ra...@liferay.com>.
What we do in our framework is push our tools into the context through a
Spring Proxy which has a MethodInterceptor wrapping the invocation in a 

try{ 
...
} catch (Throwable t) 
{
    return null
}

block.

Ray


On Thu, 2008-09-11 at 11:43 -0400, Adrian Tarau wrote:

> $! will just avoid printing a NULL value.
> 
> Best will be to wrap your objects before place them in the Velocity 
> context. Export only those methods that you will used in Velocity and 
> catch non-critical exceptions(those that can be converted to a "missing 
> value").
> 
> Or even better, I would recommend to place in the context only 
> POJOs(Plain Java Objects) and not complex/heavy weight objects. Avoid 
> lazy initializations(like Hibernate lazy initialized 
> collections/objects) because of two reasons : if it fails, it fails 
> during rendering(which should not happen, your data should be already 
> "collected", ready to be rendered) and if you measure(this matters only 
> if you measure it :) ) the page rendering time it will give you wrong 
> results(you will blame Velocity for slow rendering or just how the 
> template was done).
> 
> Prepare your data in the business layer and if nothing fails render your 
> data with Velocity to get the result page.
> Is not possible all the time to have all your data ready before 
> rendering(for performance reasons), but try to avoid that as much as 
> possible.
> 
> Nathan Bubna wrote:
> > that won't catch exceptions.  you need to use an event handler,
> > particularly the MethodExceptionEventHandler.
> >
> > On Wed, Sep 10, 2008 at 11:44 PM, Manish <pr...@gmx.net> wrote:
> >   
> >> use this -
> >>
> >> my name is: $!o.foo()
> >>
> >> rgds,
> >> - Manish
> >>
> >> ----- Original Message ----- From: "bluejoe" <bl...@sdb.cnic.cn>
> >> To: <us...@velocity.apache.org>
> >> Sent: Thursday, September 11, 2008 12:09 PM
> >> Subject: question on Velocity: How to let Velocity keep silent when some
> >> exceptions occur?
> >>
> >>
> >>     
> >>>  Hi, I am a user of Velocity, now I have a question: How to let Velocity
> >>> keep silent when some exceptions occur?
> >>>
> >>>  For example, I wrote codes:
> >>>
> >>>   my name is: $o.foo()
> >>>
> >>>  In some cases, foo() may throw some exceptions, like NullException, etc.
> >>> It seems that Velocity will throw these exceptions too. But I want Velocity
> >>> to regard the statement as:
> >>>
> >>>   my name is: <empty>
> >>>
> >>>  Is there any way to let Velocity ignore the errors?
> >>>
> >>> Sincerely Yours,
> >>> bluejoe
> >>> 2008-9-11
> >>>       
> >>
> >> ---------------------------------------------------------------------
> >> 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
> >
> >   
> 

--
Raymond Augé
Software Engineer
Liferay, Inc.
Enterprise. Open Source. For Life.


Re: question on Velocity: How to let Velocity keep silent when some exceptions occur?

Posted by Adrian Tarau <ad...@intellisoftsystems.com>.
$! will just avoid printing a NULL value.

Best will be to wrap your objects before place them in the Velocity 
context. Export only those methods that you will used in Velocity and 
catch non-critical exceptions(those that can be converted to a "missing 
value").

Or even better, I would recommend to place in the context only 
POJOs(Plain Java Objects) and not complex/heavy weight objects. Avoid 
lazy initializations(like Hibernate lazy initialized 
collections/objects) because of two reasons : if it fails, it fails 
during rendering(which should not happen, your data should be already 
"collected", ready to be rendered) and if you measure(this matters only 
if you measure it :) ) the page rendering time it will give you wrong 
results(you will blame Velocity for slow rendering or just how the 
template was done).

Prepare your data in the business layer and if nothing fails render your 
data with Velocity to get the result page.
Is not possible all the time to have all your data ready before 
rendering(for performance reasons), but try to avoid that as much as 
possible.

Nathan Bubna wrote:
> that won't catch exceptions.  you need to use an event handler,
> particularly the MethodExceptionEventHandler.
>
> On Wed, Sep 10, 2008 at 11:44 PM, Manish <pr...@gmx.net> wrote:
>   
>> use this -
>>
>> my name is: $!o.foo()
>>
>> rgds,
>> - Manish
>>
>> ----- Original Message ----- From: "bluejoe" <bl...@sdb.cnic.cn>
>> To: <us...@velocity.apache.org>
>> Sent: Thursday, September 11, 2008 12:09 PM
>> Subject: question on Velocity: How to let Velocity keep silent when some
>> exceptions occur?
>>
>>
>>     
>>>  Hi, I am a user of Velocity, now I have a question: How to let Velocity
>>> keep silent when some exceptions occur?
>>>
>>>  For example, I wrote codes:
>>>
>>>   my name is: $o.foo()
>>>
>>>  In some cases, foo() may throw some exceptions, like NullException, etc.
>>> It seems that Velocity will throw these exceptions too. But I want Velocity
>>> to regard the statement as:
>>>
>>>   my name is: <empty>
>>>
>>>  Is there any way to let Velocity ignore the errors?
>>>
>>> Sincerely Yours,
>>> bluejoe
>>> 2008-9-11
>>>       
>>
>> ---------------------------------------------------------------------
>> 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: question on Velocity: How to let Velocity keep silent when some exceptions occur?

Posted by Nathan Bubna <nb...@gmail.com>.
that won't catch exceptions.  you need to use an event handler,
particularly the MethodExceptionEventHandler.

On Wed, Sep 10, 2008 at 11:44 PM, Manish <pr...@gmx.net> wrote:
> use this -
>
> my name is: $!o.foo()
>
> rgds,
> - Manish
>
> ----- Original Message ----- From: "bluejoe" <bl...@sdb.cnic.cn>
> To: <us...@velocity.apache.org>
> Sent: Thursday, September 11, 2008 12:09 PM
> Subject: question on Velocity: How to let Velocity keep silent when some
> exceptions occur?
>
>
>>  Hi, I am a user of Velocity, now I have a question: How to let Velocity
>> keep silent when some exceptions occur?
>>
>>  For example, I wrote codes:
>>
>>   my name is: $o.foo()
>>
>>  In some cases, foo() may throw some exceptions, like NullException, etc.
>> It seems that Velocity will throw these exceptions too. But I want Velocity
>> to regard the statement as:
>>
>>   my name is: <empty>
>>
>>  Is there any way to let Velocity ignore the errors?
>>
>> Sincerely Yours,
>> bluejoe
>> 2008-9-11
>
>
>
> ---------------------------------------------------------------------
> 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: question on Velocity: How to let Velocity keep silent when some exceptions occur?

Posted by bluejoe <bl...@gmail.com>.
thank you very much:)

----- Original Message ----- 
From: "Manish" <pr...@gmx.net>
To: "Velocity Users List" <us...@velocity.apache.org>
Sent: Thursday, September 11, 2008 2:44 PM
Subject: Re: question on Velocity: How to let Velocity keep silent when some exceptions occur?


> use this -
> 
> my name is: $!o.foo()
> 
> rgds,
> - Manish
> 
> ----- Original Message ----- 
> From: "bluejoe" <bl...@sdb.cnic.cn>
> To: <us...@velocity.apache.org>
> Sent: Thursday, September 11, 2008 12:09 PM
> Subject: question on Velocity: How to let Velocity keep silent when some 
> exceptions occur?
> 
> 
>>  Hi, I am a user of Velocity, now I have a question: How to let Velocity keep 
>> silent when some exceptions occur?
>>
>>  For example, I wrote codes:
>>
>>    my name is: $o.foo()
>>
>>  In some cases, foo() may throw some exceptions, like NullException, etc. It 
>> seems that Velocity will throw these exceptions too. But I want Velocity to 
>> regard the statement as:
>>
>>    my name is: <empty>
>>
>>  Is there any way to let Velocity ignore the errors?
>>
>> Sincerely Yours,
>> bluejoe
>> 2008-9-11 
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
>

Re: question on Velocity: How to let Velocity keep silent when some exceptions occur?

Posted by Manish <pr...@gmx.net>.
use this -

my name is: $!o.foo()

rgds,
- Manish

----- Original Message ----- 
From: "bluejoe" <bl...@sdb.cnic.cn>
To: <us...@velocity.apache.org>
Sent: Thursday, September 11, 2008 12:09 PM
Subject: question on Velocity: How to let Velocity keep silent when some 
exceptions occur?


>  Hi, I am a user of Velocity, now I have a question: How to let Velocity keep 
> silent when some exceptions occur?
>
>  For example, I wrote codes:
>
>    my name is: $o.foo()
>
>  In some cases, foo() may throw some exceptions, like NullException, etc. It 
> seems that Velocity will throw these exceptions too. But I want Velocity to 
> regard the statement as:
>
>    my name is: <empty>
>
>  Is there any way to let Velocity ignore the errors?
>
> Sincerely Yours,
> bluejoe
> 2008-9-11 



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