You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by "Geir Magnusson Jr." <ge...@optonline.net> on 2001/08/07 01:11:42 UTC

Re: Design question: just trying to understand

Nick Bauman wrote:
> 
> Currently, the template rendering pattern for Velocity is such:
> ...
>     try {
>       VelocityContext context = new VelocityContext();
>       context.put("foo", objectOfSomeKind);
>       template = Velocity.getTemplate("/bar/baz/blurfl.vtl");
>       template.merge(context, sw);
>     } catch (ResourceNotFoundException rnfe) {
>       // couldn't find it
>     } catch (ParseErrorException pee) {
>       // couldn't parse it
>     }
> ...
> 
> Now, I'd like to handle exceptions in my own particular way when I call a
> method on that $foo reference in there and it turns out to be bunk. 

I think we need some clarification on the word 'bunk'.... :)

> The
> reason this is important is because the object model for that $foo may
> change from release to release of my product, and I'd like to know if that
> change is blowing the merge() call in my code, not in the Velocity log. The
> niave thing to do would be something like this:
> 
> try {
>   VelocityContext context = new VelocityContext();
>   context.put("foo", objectOfSomeKind);
>   template = Velocity.getTemplate("/bar/baz/blurfl.vtl");
>   template.merge(context, sw);
> } catch (ResourceNotFoundException rnfe) {
>   // couldn't find it
> } catch (ParseErrorException pee) {
>   // couldn't parse it
> } catch (MethodException me) {
>   // a method call on $foo is bunk
> }
> 
> But I can't do that.

Why not?  We have a MethodInvocationException that propogates out like a
Parse.. or Resource...  It is thrown when a method call in a template
throws an exception.

> With Velocity, the correct way to do it is to write a
> class that listens for that exception event and register that class with the
> context. I get it, but I'd like to know why this technique was chosen, why
> not just let the context throw that as an exception? Or maybe make it
> tunable in the config?

You have a choice.  The Event approach is for people who want to catch
the exception in-stream, and substitute a value so the rendering can
continue.
 
> Just asking. Thank you.

Does this solve your problem, or did I misinterpret something?

geir

-- 
Geir Magnusson Jr.                           geirm@optonline.net
System and Software Consulting
Developing for the web?  See http://jakarta.apache.org/velocity/
Be well, do good work, and keep in touch.

Re: newbie question...

Posted by Leon Messerschmidt <le...@opticode.co.za>.
HI Sumit,

There are some nice examples that comes with the Velocity Download.

Also:
http://www.javaworld.com/javaworld/jw-07-2001/jw-0727-templates.html

~ Leon

----- Original Message -----
From: "Sumit Ranjan" <s_...@rti.daimlerchrysler.com>
To: <ve...@jakarta.apache.org>
Sent: Tuesday, August 07, 2001 7:10 AM
Subject: newbie question...


> hi all!
>   i am new to velocity...
>   can anybody please direct me to any documentations/How Tos on velocity ?
>
> TIA
>
> sumit ranjan
>


newbie question...

Posted by Sumit Ranjan <s_...@rti.daimlerchrysler.com>.
hi all!
  i am new to velocity...
  can anybody please direct me to any documentations/How Tos on velocity ?

TIA

sumit ranjan


Re: Design question: just trying to understand

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
Nick Bauman wrote:
> 
> Geir,
> 
> >> Now, I'd like to handle exceptions in my own particular way when I
> >> call a method on that $foo reference in there and it turns out to be
> >> bunk.
> >
> > I think we need some clarification on the word 'bunk'.... :)
> 
> Well, say, if the template has something like this:
> 
> #set ($myString = "blurfl")
> $myString.indexOff("fl") ## ain't no "indexOff" in java.lang.String!

Then what I was talking about wouldn't help you, because
$myString.indexOff() won't throw an exception - it would simply be
rendered as schmoo.

> >> The
> >> reason this is important is because the object model for that $foo may
> >> change from release to release of my product, and I'd like to know if
> >> that change is blowing the merge() call in my code, not in the
> >> Velocity log. The niave thing to do would be something like this:
> >>
> >> try {
> >>   VelocityContext context = new VelocityContext();
> >>   context.put("foo", objectOfSomeKind);
> >>   template = Velocity.getTemplate("/bar/baz/blurfl.vtl");
> >>   template.merge(context, sw);
> >> } catch (ResourceNotFoundException rnfe) {
> >>   // couldn't find it
> >> } catch (ParseErrorException pee) {
> >>   // couldn't parse it
> >> } catch (MethodException me) {
> >>   // a method call on $foo is bunk
> >> }
> >>
> >> But I can't do that.
> >
> > Why not?  We have a MethodInvocationException that propogates out like
> > a Parse.. or Resource...  It is thrown when a method call in a template
> > throws an exception.
> 
> I'm dumber than I thought possible! You mean to say I _can_ just do
> that?!?! (well, catch  a MethodInvocationException, not the MethodException
> in my example) If it's just that easy, than Velocity is just that much
> cooler!!

I have a feeling that we aren't connecting :

I think that what you want is not the ability to catch an exception, but
rather know when a reference is treated like schmoo..?
 
> >> With Velocity, the correct way to do it is to write a
> >> class that listens for that exception event and register that class
> >> with the context. I get it, but I'd like to know why this technique
> >> was chosen, why not just let the context throw that as an exception?
> >> Or maybe make it tunable in the config?
> >
> > You have a choice.  The Event approach is for people who want to catch
> > the exception in-stream, and substitute a value so the rendering can
> > continue.
> 
> Woah, JSP can't touch that with a 10' pole, can it? Sorry if I sound like
> I'm preaching to the converted too much, but that is totally slick.

No - that's certainly an area where Velocity and WebMacro shine, in
error handling....
 
> >> Just asking. Thank you.
> >
> > Does this solve your problem, or did I misinterpret something?
> 
> I believe it does! I'll implement it posthaste and report back. Thank you!

I have a feeling that we misunderstood each other - that the MIE won't
solve your problem...
 
> > geir
> >
> > --
> > Geir Magnusson Jr.                           geirm@optonline.net
> > System and Software Consulting
> > Developing for the web?  See http://jakarta.apache.org/velocity/
> > Be well, do good work, and keep in touch.
> 
> Garrison couldn't have said it better, but you're one of them east-coasters!
> ;)

Hey!  Good catch.  I keep thinking I should attribute it. I don't think
of it as a brilliant or funny insight than just a really nice sentiment.
I guess I should.

My family has listened to him ever since I can remember.  (We're a bunch
of commie-pinko NPR/PRI supporters... :)  My father is a huge fan of
Prairie Home, listens to it every week.  I catch the Writers Almanac
every now and then driving, which is where I got the quote...

geir

-- 
Geir Magnusson Jr.                           geirm@optonline.net
System and Software Consulting
Developing for the web?  See http://jakarta.apache.org/velocity/
Be well, do good work, and keep in touch.

Re: Design question: just trying to understand

Posted by Nick Bauman <ni...@cortexity.com>.
Geir,

>> Now, I'd like to handle exceptions in my own particular way when I
>> call a method on that $foo reference in there and it turns out to be
>> bunk. 
> 
> I think we need some clarification on the word 'bunk'.... :)

Well, say, if the template has something like this:

#set ($myString = "blurfl")
$myString.indexOff("fl") ## ain't no "indexOff" in java.lang.String!
 
>> The
>> reason this is important is because the object model for that $foo may
>> change from release to release of my product, and I'd like to know if
>> that change is blowing the merge() call in my code, not in the
>> Velocity log. The niave thing to do would be something like this:
>> 
>> try {
>>   VelocityContext context = new VelocityContext();
>>   context.put("foo", objectOfSomeKind);
>>   template = Velocity.getTemplate("/bar/baz/blurfl.vtl");
>>   template.merge(context, sw);
>> } catch (ResourceNotFoundException rnfe) {
>>   // couldn't find it
>> } catch (ParseErrorException pee) {
>>   // couldn't parse it
>> } catch (MethodException me) {
>>   // a method call on $foo is bunk
>> }
>> 
>> But I can't do that.
> 
> Why not?  We have a MethodInvocationException that propogates out like
> a Parse.. or Resource...  It is thrown when a method call in a template
> throws an exception.

I'm dumber than I thought possible! You mean to say I _can_ just do 
that?!?! (well, catch  a MethodInvocationException, not the MethodException 
in my example) If it's just that easy, than Velocity is just that much 
cooler!!

>> With Velocity, the correct way to do it is to write a
>> class that listens for that exception event and register that class
>> with the context. I get it, but I'd like to know why this technique
>> was chosen, why not just let the context throw that as an exception?
>> Or maybe make it tunable in the config?
> 
> You have a choice.  The Event approach is for people who want to catch
> the exception in-stream, and substitute a value so the rendering can
> continue.

Woah, JSP can't touch that with a 10' pole, can it? Sorry if I sound like 
I'm preaching to the converted too much, but that is totally slick.
  
>> Just asking. Thank you.
> 
> Does this solve your problem, or did I misinterpret something?

I believe it does! I'll implement it posthaste and report back. Thank you!
 
> geir
> 
> -- 
> Geir Magnusson Jr.                           geirm@optonline.net
> System and Software Consulting
> Developing for the web?  See http://jakarta.apache.org/velocity/
> Be well, do good work, and keep in touch.

Garrison couldn't have said it better, but you're one of them east-coasters!
;)

-- 
Nick Bauman
3600 N. Dupont
Minneapolis, MN 55412
Mobile: (612) 232-7120
Home: (612) 522-0165


Re: Design question: just trying to understand

Posted by Nick Bauman <ni...@cortexity.com>.
Yes, that does solve my problem. Didn't know it was that easy. Which makes 
Velocity that much cooler. I'll implement it and report back. Thanks.

-- 
Nick Bauman
3600 N. Dupont
Minneapolis, MN 55412
Mobile: (612) 232-7120
Home: (612) 522-0165


Re: Design question [again]

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
Nick Bauman wrote:
> 
> I guess that doesn't work for me, after all. What I'm trying to catch is this:
> 
> Tue Aug 07 16:20:52 CDT 2001   [warn]
> org.apache.velocity.runtime.exception.ReferenceException: reference :
> template = msneshopuk/Htmlen_msnruksh.vtl [line 38,column 1] :
> $myString.indexOff("o") is not a valid reference.
> 
> I want to be able to handle this in my code instead of it only going through
> Velocity's logging API. If that reference is just bogus, I'd like to know in
> my own code as the result of the merge. How does one do that?

I figured that I misunderstood.  Yes, we will have to add an event
handler for this.

geir

> - Thanks
> 
> > Nick Bauman wrote:
> >>
> >> Currently, the template rendering pattern for Velocity is such:
> >> ...
> >>     try {
> >>       VelocityContext context = new VelocityContext();
> >>       context.put("foo", objectOfSomeKind);
> >>       template = Velocity.getTemplate("/bar/baz/blurfl.vtl");
> >>       template.merge(context, sw);
> >>     } catch (ResourceNotFoundException rnfe) {
> >>       // couldn't find it
> >>     } catch (ParseErrorException pee) {
> >>       // couldn't parse it
> >>     }
> >> ...
> >>
> >> Now, I'd like to handle exceptions in my own particular way when I
> >> call a method on that $foo reference in there and it turns out to be
> >> bunk.
> >
> > I think we need some clarification on the word 'bunk'.... :)
> >
> >> The
> >> reason this is important is because the object model for that $foo may
> >> change from release to release of my product, and I'd like to know if
> >> that change is blowing the merge() call in my code, not in the
> >> Velocity log. The niave thing to do would be something like this:
> >>
> >> try {
> >>   VelocityContext context = new VelocityContext();
> >>   context.put("foo", objectOfSomeKind);
> >>   template = Velocity.getTemplate("/bar/baz/blurfl.vtl");
> >>   template.merge(context, sw);
> >> } catch (ResourceNotFoundException rnfe) {
> >>   // couldn't find it
> >> } catch (ParseErrorException pee) {
> >>   // couldn't parse it
> >> } catch (MethodException me) {
> >>   // a method call on $foo is bunk
> >> }
> >>
> >> But I can't do that.
> >
> > Why not?  We have a MethodInvocationException that propogates out like
> > a Parse.. or Resource...  It is thrown when a method call in a template
> > throws an exception.
> >
> >> With Velocity, the correct way to do it is to write a
> >> class that listens for that exception event and register that class
> >> with the context. I get it, but I'd like to know why this technique
> >> was chosen, why not just let the context throw that as an exception?
> >> Or maybe make it tunable in the config?
> >
> > You have a choice.  The Event approach is for people who want to catch
> > the exception in-stream, and substitute a value so the rendering can
> > continue.
> >
> >> Just asking. Thank you.
> >
> > Does this solve your problem, or did I misinterpret something?
> >
> > geir
> >
> > --
> > Geir Magnusson Jr.                           geirm@optonline.net
> > System and Software Consulting
> > Developing for the web?  See http://jakarta.apache.org/velocity/
> > Be well, do good work, and keep in touch.
> 
> --
> Nick Bauman
> 3600 N. Dupont
> Minneapolis, MN 55412
> Mobile: (612) 232-7120
> SMS: 6122327120@voicestream.net
> Home: (612) 522-0165

-- 
Geir Magnusson Jr.                           geirm@optonline.net
System and Software Consulting
Developing for the web?  See http://jakarta.apache.org/velocity/
Well done is better than well said - New England Proverb

Re: Design question [again]

Posted by Nick Bauman <ni...@cortexity.com>.
I guess that doesn't work for me, after all. What I'm trying to catch is this:

Tue Aug 07 16:20:52 CDT 2001   [warn]
org.apache.velocity.runtime.exception.ReferenceException: reference :
template = msneshopuk/Htmlen_msnruksh.vtl [line 38,column 1] :
$myString.indexOff("o") is not a valid reference.

I want to be able to handle this in my code instead of it only going through
Velocity's logging API. If that reference is just bogus, I'd like to know in
my own code as the result of the merge. How does one do that?

- Thanks

> Nick Bauman wrote:
>> 
>> Currently, the template rendering pattern for Velocity is such:
>> ...
>>     try {
>>       VelocityContext context = new VelocityContext();
>>       context.put("foo", objectOfSomeKind);
>>       template = Velocity.getTemplate("/bar/baz/blurfl.vtl");
>>       template.merge(context, sw);
>>     } catch (ResourceNotFoundException rnfe) {
>>       // couldn't find it
>>     } catch (ParseErrorException pee) {
>>       // couldn't parse it
>>     }
>> ...
>> 
>> Now, I'd like to handle exceptions in my own particular way when I
>> call a method on that $foo reference in there and it turns out to be
>> bunk. 
> 
> I think we need some clarification on the word 'bunk'.... :)
> 
>> The
>> reason this is important is because the object model for that $foo may
>> change from release to release of my product, and I'd like to know if
>> that change is blowing the merge() call in my code, not in the
>> Velocity log. The niave thing to do would be something like this:
>> 
>> try {
>>   VelocityContext context = new VelocityContext();
>>   context.put("foo", objectOfSomeKind);
>>   template = Velocity.getTemplate("/bar/baz/blurfl.vtl");
>>   template.merge(context, sw);
>> } catch (ResourceNotFoundException rnfe) {
>>   // couldn't find it
>> } catch (ParseErrorException pee) {
>>   // couldn't parse it
>> } catch (MethodException me) {
>>   // a method call on $foo is bunk
>> }
>> 
>> But I can't do that.
> 
> Why not?  We have a MethodInvocationException that propogates out like
> a Parse.. or Resource...  It is thrown when a method call in a template
> throws an exception.
> 
>> With Velocity, the correct way to do it is to write a
>> class that listens for that exception event and register that class
>> with the context. I get it, but I'd like to know why this technique
>> was chosen, why not just let the context throw that as an exception?
>> Or maybe make it tunable in the config?
> 
> You have a choice.  The Event approach is for people who want to catch
> the exception in-stream, and substitute a value so the rendering can
> continue.
>  
>> Just asking. Thank you.
> 
> Does this solve your problem, or did I misinterpret something?
> 
> geir
> 
> -- 
> Geir Magnusson Jr.                           geirm@optonline.net
> System and Software Consulting
> Developing for the web?  See http://jakarta.apache.org/velocity/
> Be well, do good work, and keep in touch.


-- 
Nick Bauman
3600 N. Dupont
Minneapolis, MN 55412
Mobile: (612) 232-7120
SMS: 6122327120@voicestream.net
Home: (612) 522-0165