You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Rocchi Cesare <ro...@itc.it> on 2004/10/15 11:05:19 UTC
Can't access public fields
Hiall,
I have defined a Restaurant class like this:
public class Restaurant {
public String name = "";
public String address = "";
}
As you can see fields are public, but as far as I tried I can't access
those fields from a velocity context. If I put the rest in a context like:
Restaurant r = new Restaurant();
r.name = "test";
r.address = "bla bla ";
context.put("rest", r);
there is no way to get the value of address by means of
$rest.address
I thought it was possible. Is it? Otherwise I should convert my class
into an hashtable. That works, already tried.
Thanks,
-c.
--
+---------------------------------------+
Cesare Rocchi
ITC-IRST Povo I-38050 (TRENTO) ITALY
http://tcc.itc.it/people/rocchi.html
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org
Re: Can't access public fields
Posted by Shinobu Kawai <sh...@gmail.com>.
Hi Dimitrios,
> Velocity supports dynamic introspection (see class Uberspect for more
> details). If you want it to support such behaviour you should create
> your own introspection class (probably by copying-pasting from the
> default) and plug it into Velocity.
Curiosity kills cats and time...
Tried one. Works when it's meant to work. Haven't tested when it's
meant to fail, though. ;)
public class PublicFieldUberspectImpl extends UberspectImpl {
public VelPropertyGet getPropertyGet(Object obj, String identifier, Info i)
throws Exception {
VelPropertyGet getter = super.getPropertyGet(obj, identifier, i);
try {
getter.getMethodName();
return getter;
} catch (NullPointerException notFound) {
}
final Field field = obj.getClass().getField(identifier);
if (field != null) {
AbstractExecutor executor = new AbstractExecutor() {
public Object execute(Object o) throws IllegalAccessException, InvocationTargetException {
return field.get(o);
}
};
return new VelGetterImpl(executor);
}
return null;
}
}
Best regards,
-- Shinobu Kawai
--
Shinobu Kawai <sh...@gmail.com>
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org
Re: Can't access public fields
Posted by Dimitrios Kolovos <ds...@gmail.com>.
Velocity supports dynamic introspection (see class Uberspect for more
details). If you want it to support such behaviour you should create
your own introspection class (probably by copying-pasting from the
default) and plug it into Velocity.
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org
Re: Can't access public fields
Posted by Shinobu Kawai <sh...@gmail.com>.
Hi Peter,
> > there is no way to get the value of address by means of
> >
> > $rest.address
>
> I guess there was some config to enable/disable that, but I'm not sure ?
No, there isn't any. But for accessing static fields, you can use
FieldMethodizer.
http://jakarta.apache.org/velocity/api/org/apache/velocity/app/FieldMethodizer.html
Best regards,
-- Shinobu Kawai
--
Shinobu Kawai <sh...@gmail.com>
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org
Re: Can't access public fields
Posted by Peter Lammich <la...@ls5.cs.uni-dortmund.de>.
Rocchi Cesare wrote:
> Hiall,
>
> I have defined a Restaurant class like this:
>
> public class Restaurant {
> public String name = "";
> public String address = "";
> }
>
> As you can see fields are public, but as far as I tried I can't access
> those fields from a velocity context. If I put the rest in a context
> like:
>
> Restaurant r = new Restaurant();
> r.name = "test";
> r.address = "bla bla ";
> context.put("rest", r);
>
> there is no way to get the value of address by means of
>
> $rest.address
I guess there was some config to enable/disable that, but I'm not sure ?
>
> I thought it was possible. Is it? Otherwise I should convert my class
> into an hashtable. That works, already tried.
You could also define public getter methods, named getAddress(), getName()
Then you should be able to access $rest.address
>
> Thanks,
>
> -c.
>
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org
RE: Can't access public fields
Posted by Kalyani Kale <ka...@ascindia.com>.
Does your Restaurant class contain getAddress method? Since address is a
public field, u must not have written a getter method for it.
When u say, $r.address, velocity will try to call getAddress method on
object r (provided r is not a hashtable).
In case of hashtable, u can directly use key names to get the value.
http://jakarta.apache.org/velocity/user-guide.html#Properties
Kalyani
-----Original Message-----
From: Rocchi Cesare [mailto:rocchi@itc.it]
Sent: Friday, October 15, 2004 2:35 PM
To: Velocity Users List
Subject: Can't access public fields
Hiall,
I have defined a Restaurant class like this:
public class Restaurant {
public String name = "";
public String address = "";
}
As you can see fields are public, but as far as I tried I can't access
those fields from a velocity context. If I put the rest in a context like:
Restaurant r = new Restaurant();
r.name = "test";
r.address = "bla bla ";
context.put("rest", r);
there is no way to get the value of address by means of
$rest.address
I thought it was possible. Is it? Otherwise I should convert my class
into an hashtable. That works, already tried.
Thanks,
-c.
--
+---------------------------------------+
Cesare Rocchi
ITC-IRST Povo I-38050 (TRENTO) ITALY
http://tcc.itc.it/people/rocchi.html
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org