You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by David Parks <da...@yahoo.com> on 2011/02/18 07:02:17 UTC
Access public properties
If I have an object such as:
$thing
Which is the following class-type:
public class Thing {
public String theThing = "Something stringy";
}
And I place a Thing object in the context as variable $thing, I cannot
access:
$thing.theThing
L booo hooo
Or did I miss something? Seems like public properties would be naturally
supported.
If I add a public getter this works fine. So I have a perfectly fine
workaround.
I know getters/setters are good practice, but for internal stuff that won't
be extended I sometimes package things like compound return statements into
a simple object with a few public properties for easy access. Seems
reasonable that Velocity would check for a public property if the getter
isn't there.
Re: Access public properties
Posted by Nathan Bubna <nb...@gmail.com>.
Actually, you can use public static final properties (constants) by
putting the class in the context:
context.put("Boolean", Boolean.class);
$Boolean.TRUE
since 1.6, i believe.
On Wed, Feb 23, 2011 at 7:20 AM, Christopher Schultz
<ch...@christopherschultz.net> wrote:
> David,
>
> On 2/18/2011 1:02 AM, David Parks wrote:
>> If I have an object such as:
>> public class Thing {
>> public String theThing = "Something stringy";
>> }
>>
>> And I place a Thing object in the context as variable $thing, I cannot
>> access:
>>
>> $thing.theThing
>
> http://velocity.apache.org/engine/releases/velocity-1.7/user-guide.html
>
> See the section "Case Substitution". This section really ought to be
> called something like "resolving data references" or something like that.
>
> At any rate, Velocity uses bean-style introspection to discover
> attributes, and attributes are never members: they are always methods.
>
>> Or did I miss something? Seems like public properties would be naturally
>> supported.
>
> Unfortunately not. It also means that you can't use things like
> Boolean.TRUE in your templates. :(
>
>> Seems reasonable that Velocity would check for a public property if
>> the getter isn't there.
>
> It's probably not going to happen, but you are free to modify the
> introspector to suit your own needs. And, as always, patches are always
> welcome.
>
> -chris
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org
Re: Access public properties
Posted by Christopher Schultz <ch...@christopherschultz.net>.
David,
On 2/18/2011 1:02 AM, David Parks wrote:
> If I have an object such as:
> public class Thing {
> public String theThing = "Something stringy";
> }
>
> And I place a Thing object in the context as variable $thing, I cannot
> access:
>
> $thing.theThing
http://velocity.apache.org/engine/releases/velocity-1.7/user-guide.html
See the section "Case Substitution". This section really ought to be
called something like "resolving data references" or something like that.
At any rate, Velocity uses bean-style introspection to discover
attributes, and attributes are never members: they are always methods.
> Or did I miss something? Seems like public properties would be naturally
> supported.
Unfortunately not. It also means that you can't use things like
Boolean.TRUE in your templates. :(
> Seems reasonable that Velocity would check for a public property if
> the getter isn't there.
It's probably not going to happen, but you are free to modify the
introspector to suit your own needs. And, as always, patches are always
welcome.
-chris
Re: Access public properties
Posted by Nathan Bubna <nb...@gmail.com>.
On Wed, Feb 23, 2011 at 12:04 PM, Barbara Baughman
<ba...@utdallas.edu> wrote:
..
> Instead of trying to anticipate any security concerns down the road about
> exposing a public field, just follow best practice and provide a getter
> method. I think there is also a VelocityTool that exposes public fields.
...
that and more, for those who want it...
http://velocity.apache.org/tools/devel/javadoc/org/apache/velocity/tools/generic/ClassTool.html
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org
Re: Access public properties
Posted by Barbara Baughman <ba...@utdallas.edu>.
I disagree. My public fields are all final, immutable objects that are
essential constants within the application. Those in general should not
be visible outside the application. Therefore, having public fields
hidden to templates gets my vote.
Instead of trying to anticipate any security concerns down the road
about exposing a public field, just follow best practice and provide a
getter method. I think there is also a VelocityTool that exposes public
fields.
Barbara Baughman
Systems Analyst
Information Security
972-883-2157
On 2/23/11 10:42 AM, Thomas Vandahl wrote:
> On 18.02.2011 07:02, David Parks wrote:
>> I know getters/setters are good practice, but for internal stuff that
>> won't
>> be extended I sometimes package things like compound return
>> statements into
>> a simple object with a few public properties for easy access. Seems
>> reasonable that Velocity would check for a public property if the getter
>> isn't there.
>
> See
> http://velocity.apache.org/engine/releases/velocity-1.7/apidocs/org/apache/velocity/app/FieldMethodizer.html
>
> Bye, Thomas.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org
Re: Access public properties
Posted by Thomas Vandahl <th...@tewisoft.de>.
On 18.02.2011 07:02, David Parks wrote:
> I know getters/setters are good practice, but for internal stuff that won't
> be extended I sometimes package things like compound return statements into
> a simple object with a few public properties for easy access. Seems
> reasonable that Velocity would check for a public property if the getter
> isn't there.
See
http://velocity.apache.org/engine/releases/velocity-1.7/apidocs/org/apache/velocity/app/FieldMethodizer.html
Bye, Thomas.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org