velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Christoph Reck <apa...@recks.org>
Subject Re: best practice question
Date Wed, 02 Mar 2005 20:29:10 GMT
Hi,

I remember having problems with the introspector when accessing
static methods of a class itself. But somehow I cannot believe
that it is causing problems when the class was instantiated.

If you tested removing the "static" from the getter, and it still
does not work:
     public String getPROPERTY() {
         return PROPERTY;
     }
Then my guess goes to the velocity treatment of getters, where
the first letter is uppercased (I don't know if the rest is touched...)
but anyhow, if you tested $const.getPROPERTY() and it also did
not work, then something is really strange...

You might test the following workaround, by adding this to the
class:
     public String get(String key) {
         return "PROPERTY".equals(key) ? PROPERTY
             /* "PROPERTY2".equals(key) ? PROPERTY2 */
             /* more if cascades here... */
                : "";
     }

Somewhere in the velocity contributions there is a FieldIntrospector,
that should help you to avoid the getters totaly. Try searching the
mailing list archives or browsing the SVN repository for it. Also
there was something on setting up the Ueberspector allowing a chain
of user defined introspectors.

Cheers,
Christoph


Matthew Van Horn wrote:
> I use a Constants class in my application to hold a bunch of strings 
> used for things like request attribute keys etc. In my templates, I 
> would also like to eliminate the hard-coded strings, and I thought I 
> could do that by using the Constants class as a tool.
> 
> <tool>
>     <key>const</key>
>     <scope>application</scope>
>     <class>com.foo.util.Constants</class>
> </tool>
> 
> public class Constants {
> 
>     public static final String PROPERTY = "my.property";
>     //etc..
>     
>     //I even tried this:
>     public static String getPROPERTY() {
>         return PROPERTY;
>     }
> }
> 
> in my template
> $const.PROPERTY
> $const.getPROPERTY()
> both turn up as invalid references.
> 
> but $const.toString() works, so an object must be there.
> 
> Is this something to do with static methods? I added a non-static method 
> to test, but that didn't work either.
> 
> Do I need to implement the ViewTool interface? What should I do in 
> init() then?
> 
> According to the docs available I thought this was possible, but now I 
> am stumped. Hope I won't feel too stupid upon figuring this out.
> 
> Thanks for any help,
> Matt Van Horn


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


Mime
View raw message