You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@turbine.apache.org by Gary Baker <ga...@movielink.com> on 2003/11/06 03:00:24 UTC

Referencing java constants from with a velocity template

I created a class that holds a bunch of constants that I want to be able
to access from within my templates. (in particular, for referencing
permission strings - but that's not the point)

e.g.

<code>

package com.mycompany.util;
public class MyConstants {

	public String MY_CONSTANT = "foo";

}

</code>


To accomplish this, I added it as a pull tool in my tr.props...

tool.global.constants = com.mycompany.util.MyConstants;

The pull tool part works fine, and I get an instance of MyConstants
accessible from any template.  However, I can't get at the field.

Is it possible to reference the MY_CONSTANT field directly.

It seems that velocity always wants to call a method (getter) and won't
just return the field value.  Is this the case?  Anyone have a decent
work-around for this short of providing a getter for each constant?

-Gary



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


Re: Referencing java constants from with a velocity template

Posted by CP Lim <cp...@redsheriff.com>.
Hi Gary,

Have a look at this:

http://jakarta.apache.org/velocity/developer-guide.html#FAQ1

It describes what you want to do.  However, I think you'll need to make 
it 'public static String MY_CONSTANT ="foo";' instead.

cheers,
CP

Gary Baker wrote:

> I created a class that holds a bunch of constants that I want to be able
> to access from within my templates. (in particular, for referencing
> permission strings - but that's not the point)
> 
> e.g.
> 
> <code>
> 
> package com.mycompany.util;
> public class MyConstants {
> 
> 	public String MY_CONSTANT = "foo";
> 
> }
> 
> </code>
> 
> 
> To accomplish this, I added it as a pull tool in my tr.props...
> 
> tool.global.constants = com.mycompany.util.MyConstants;
> 
> The pull tool part works fine, and I get an instance of MyConstants
> accessible from any template.  However, I can't get at the field.
> 
> Is it possible to reference the MY_CONSTANT field directly.
> 
> It seems that velocity always wants to call a method (getter) and won't
> just return the field value.  Is this the case?  Anyone have a decent
> work-around for this short of providing a getter for each constant?
> 
> -Gary
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: turbine-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: turbine-user-help@jakarta.apache.org
> 
> 
> 

-- 
R E D S H E R I F F
C.P. Lim - Software Engineer
[cp_lim@redsheriff.com]

96-98 Market St    +61 (3) 9606 4036 tel
South Melbourne    +61 (3) 9606 4001 fax
Victoria  3205     +61 (413) 781 846 cell

This message and any files transmitted with it are confidential and 
intended solely for the use of the individual or entity to whom they are 
addressed. If you are not the intended recipient, you are hereby 
notified that any use or dissemination of this communication is strictly 
prohibited. If you have received this message in error please notify us 
immediately by return email or telephone +61 (2) 8204 5888, then delete 
this message. Any views expressed in this message are those of the 
individual sender and many not necessarily reflect the views of Red 
Sheriff.


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