velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Will Glass-Husain" <>
Subject Re: object type casting in velocity context
Date Sun, 03 Jul 2005 01:48:25 GMT
In a word, no.  Velocity provides a lot of flexibility in that you can call 
any Java method from the template, but this means that you need to be 
careful as to which methods are available.

Best bet is to make a wrapper object and pass through the methods you are 
allowing to be called.


----- Original Message ----- 
From: "Allen Gilliland" <>
To: <>
Sent: Saturday, July 02, 2005 3:26 PM
Subject: object type casting in velocity context

Hopefully this hasn't been asked a million times before, but I didn't
seen anything that asked this exact question from my search of the
list archives.

So, is there a way to control the object type of something in the
velocity context?  basically i would like to down cast some of my
objects into parent classes or interfaces so that i can limit what
methods are available inside templates.

an example would be ...

// safe version
public class FooObject {
   public String getXXX();
   public String getYYY();

// complete version
public class FooObjectImpl extends FooObject {
   public String getXXX() {}
   public String getYYY() {}
   public void setXXX(String x) {}
   public void setYYY(String y) {}
   public void save() {}
   public void remove() {}

then when i am dealing with velocity i can do this ...

FooObjectImpl fooInstance = new FooObjectImpl();
context.put("foo", (FooObject) fooInstance);

this would be extremely valuable to me because it would allow me to
use my existing set of pojos without exposting setXXX() or other
potentially dangerous public methods to template writers.

has anyone done something like this before?  maybe something like this? ...

// ( name, Object, cast to object type);
context.put("foo", fooInstance, FooObject.class);

-- Allen

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

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

View raw message