velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Bill Boland" <>
Subject Rules for property lookup and isXxx() support
Date Wed, 03 Jul 2002 15:37:26 GMT

I noticed a problem with a template I created that used a boolean
property (named newItem) of bean (foo) in the context. I placed
$foo.newItem into a template and it returned an empty String value. If I
called the method $foo.isNewItem(), it worked just fine returning "true"
or "false". I noticed in some archived messages that the introspection
of isXxx() was not supported as well as full support for the JavaBean
specification but this was a while ago. Is it still unsupported or am I
missing something? 

My assumption (I know...shame on me) was that it followed the
specification since the documentation doesn't explicitly say that
properties are derived from only "getXxxx()" methods. 

[Doing some researching/reading while writing this...]

I think that in my case, $foo also has a "get()" method to support a Map
lookup into a dynamic property collection and it finds this in the case
where $foo.newItem is specified (calling the equiv. of $foo.get(
"newItem" ) instead of $foo.isNewItem()). I didn't even know this syntax
feature existed for Maps! It's right there in the documentation but I
easily overlooked it. Maybe that is why it returns an empty string?  

So the precedence of the rules for properties in Velocity are just
different but not explicitly documented. I was able to get Boolean
values using isXxx() methods from other objects that did not have a
get() method (example: $req.isSecure() in HttpServletRequest object),
but not my bean. Once I add a getXxxx() method to my bean (along with
the isXxx() method), I am able to get the property using $ so
this must have precedence over the get() lookup for Maps. Luckily, I am
in control of the methods for this bean (in this case) but I may not be
in the future. The empty string was being returned from the get() method
when the "newItem" key was not found in the internal Map.

Below is what I think the internal rules are for mapping the property
name to a method on the bean. Could someone confirm this or document the
lookup rules for properties or tell me where I'm going wrong? If this is
correct, shouldn't (2) and (3) be reversed so that get() is only used if
getXxx() AND isXxx() are not specified? 

(1) See if the bean has a getXxx() method where Xxx is the property
name. If so, use that method to get the value. 
(2) See if the bean has a get( Object ) method. If so, use the property
name as a key to this method and return the value.
(3) See if the bean has an isXxx() method where Xxx is the property
name. If so use that method to get the value.

I know I can explicitly get around these rules by specifying the method
and not the property but I felt that (1) if I've come across this,
someone else is bound to at some time or another and (2) it should be
documented since it doesn't follow the JavaBean spec.

Using 1.3-rc1 of Velocity.

Thanks for any help.


To unsubscribe, e-mail:   <>
For additional commands, e-mail: <>

View raw message