Re: best practice question
Hi Matthew, > 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. For the time being, you can try out the FieldMethodizer. http://jakarta.apache.org/velocity/api/org/apache/velocity/app/FieldMethodizer.html Or the PublicFieldUberspect. http://wiki.apache.org/jakarta-velocity/PublicFieldUberspect Best regards, -- Shinobu -- Shinobu Kawai <[EMAIL PROTECTED]> - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: best practice question
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. const application com.foo.util.Constants 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: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: best practice question
that's very strange. what you're doing looks fine to me and definitely should work. and no, it shouldn't make a difference whether the methods are static or not. they just need to be public methods in public classes. so, the first thing i suggest double-checking is that the Constants class is declared public. if it's not, it would explain why toString() works, but your methods don't (toString() is in Object, which is declared public). if that's not the problem, then would you send your Constants class itself for us to try this out? if you're not comfortable or allowed to show it to the public list, you could just send it to me, and i'll try it out. On Wed, 02 Mar 2005 08:58:35 -0500, Matthew Van Horn <[EMAIL PROTECTED]> 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. > > > const > application > com.foo.util.Constants > > > 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: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > > - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]