You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by piero de salvia <pi...@yahoo.com> on 2001/07/03 00:31:53 UTC

ReferenceException

Hi guys,

It's one of those days when nothing works...

I have:

public class Hyperlink {

	public String	Location;
	public String	Label;
}

and in my servlet I do:

Hyperlink hl = new Hyperlink();
hl.Label = "/site/section"; 
hl.Location = "Section";
ctx.put("hl", hl);

and in my template I do:

<a href="$hl.Location">$hl.Label</a>

and my result is:

<a href="$hl.Location">$hl.Label</a>


plus 2 ReferenceExceptions:

Tue Jul 03 00:29:21 GMT+02:00 2001   [warn]
org.apache.velocity.runtime.exception.ReferenceException:
reference : template = /piergi/layout/navbar.vm [line
6,column 42] : $hl.Location is not a valid reference.
Tue Jul 03 00:29:21 GMT+02:00 2001   [warn]
org.apache.velocity.runtime.exception.ReferenceException:
reference : template = /piergi/layout/navbar.vm [line
6,column 56] : $hl.Label is not a valid reference.


Should I give up programming?

piero de salvia

__________________________________________________
Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail
http://personal.mail.yahoo.com/

Re: ReferenceException

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
piero de salvia wrote:
> 
> Hi guys,
> 
> It's one of those days when nothing works...

Monday...

> I have:
> 
> public class Hyperlink {
> 
>         public String   Location;
>         public String   Label;
> }
> 
> and in my servlet I do:
> 
> Hyperlink hl = new Hyperlink();
> hl.Label = "/site/section";
> hl.Location = "Section";
> ctx.put("hl", hl);
> 
> and in my template I do:
> 
> <a href="$hl.Location">$hl.Label</a>
> 
> and my result is:
> 
> <a href="$hl.Location">$hl.Label</a>
> 
> plus 2 ReferenceExceptions:
> 
> Tue Jul 03 00:29:21 GMT+02:00 2001   [warn]
> org.apache.velocity.runtime.exception.ReferenceException:
> reference : template = /piergi/layout/navbar.vm [line
> 6,column 42] : $hl.Location is not a valid reference.
> Tue Jul 03 00:29:21 GMT+02:00 2001   [warn]
> org.apache.velocity.runtime.exception.ReferenceException:
> reference : template = /piergi/layout/navbar.vm [line
> 6,column 56] : $hl.Label is not a valid reference.
> 
> Should I give up programming?

You could.  It wouldn't solve the problem though.

The problem is that Velocity will not let you get to members, just
methods.  So either make a set of getter/setter methods

public String getLocation()

etc

and all will work, or use the FieldMethodizer class to do fake it for
you.

geir

-- 
Geir Magnusson Jr.                           geirm@optonline.net
System and Software Consulting
Developing for the web?  See http://jakarta.apache.org/velocity/
You have a genius for suggesting things I've come a cropper with!

Re: ReferenceException

Posted by "Patrick E. Whitesell" <pw...@clickability.com>.
piero de salvia wrote:

>Hi guys,
>
>It's one of those days when nothing works...
>
>I have:
>
>public class Hyperlink {
>
>	public String	Location;
>	public String	Label;
>}
>
>and in my servlet I do:
>
>Hyperlink hl = new Hyperlink();
>hl.Label = "/site/section"; 
>hl.Location = "Section";
>ctx.put("hl", hl);
>
>and in my template I do:
>
><a href="$hl.Location">$hl.Label</a>
>
>and my result is:
>
><a href="$hl.Location">$hl.Label</a>
>
>
>plus 2 ReferenceExceptions:
>
>Tue Jul 03 00:29:21 GMT+02:00 2001   [warn]
>org.apache.velocity.runtime.exception.ReferenceException:
>reference : template = /piergi/layout/navbar.vm [line
>6,column 42] : $hl.Location is not a valid reference.
>Tue Jul 03 00:29:21 GMT+02:00 2001   [warn]
>org.apache.velocity.runtime.exception.ReferenceException:
>reference : template = /piergi/layout/navbar.vm [line
>6,column 56] : $hl.Label is not a valid reference.
>
>
>Should I give up programming?
>
>piero de salvia
>
>__________________________________________________
>Do You Yahoo!?
>Get personalized email addresses from Yahoo! Mail
>http://personal.mail.yahoo.com/
>
You need getLocation() and getLabel() methods in your Hyperlink class. 
 The Velocity engine doesn't access the properties of Objects, it uses 
get/set methods to access them (as per the Beans spec).

So

$h1.Location

is interpreted as

h1.getLocation()



#set($h1.Location = "here")

would be interpreted as

h1.setLocation("here")

-- 
Patrick E. Whitesell
pwhitesell@clickability.com

***************************************
This e-mail message may contain confidential and/or legally privileged information. If you are not the intended recipient(s), or the employee or agent responsible for delivery of this message to the intended recipient(s), you are hereby notified that any dissemination, distribution or copying of this e-mail message is strictly prohibited.If you have received this message in error, please immediately notify the sender and delete this e-mail message from your computer. DO NOT FORWARD THIS E-MAIL MESSAGE WITHOUT THE PERMISSION OF THE SENDER.




Re: how to use custom velocity.properties

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
daljeetsingh wrote:
> 
> Do i have to add this setting for the servlets that extend VelocityServlet?
> 
> What I want to do is tell velocity to use a custom properties file for
> all the Velocity related class files.
> 
> One other way is to extend the VelocityServlet in one of the classes,
> add this class to web.xml and extend this class further on.
> 
> Would that be the right approach?

In order to use Velocity in the servlet environment, either your servlet
needs to extend VelocityServlet, or your servlet needs to do most of the
things that VelocityServlet does.

So it's easier to extend VelocityServlet.

Did you review the servlet_example2 and servlet_example2 in the examples
directory in the distribution?

geir

-- 
Geir Magnusson Jr.                           geirm@optonline.net
System and Software Consulting
Developing for the web?  See http://jakarta.apache.org/velocity/
You have a genius for suggesting things I've come a cropper with!

Re[2]: how to use custom velocity.properties

Posted by daljeetsingh <da...@softhome.net>.
Do i have to add this setting for the servlets that extend VelocityServlet?

What I want to do is tell velocity to use a custom properties file for
all the Velocity related class files.

One other way is to extend the VelocityServlet in one of the classes,
add this class to web.xml and extend this class further on.

Would that be the right approach?

Re: how to use custom velocity.properties

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
daljeetsingh wrote:
> 
> Hello
> 
> I need to use a custom velocity.properties file for all the servlets in
> a web application.
> 
> In order to do that I added the following to the web.xml
>     <servlet >
>       <servlet-name>org.apache.velocity.servlet.VelocityServlet</servlet-name>
>       <servlet-class>org.apache.velocity.servlet.VelocityServlet</servlet-class>
>       <init-param>
>          <param-name>properties</param-name>
>          <param-value>velocity.properties</param-value>
>       </init-param>
>     </servlet>
> 
> This change has not been sufficient for the velocity servlet to use the
> custom properties file in the loadConfiguration method ??
> 
> What's wrong?

1) I would change <servlet-name> to something else, like 'MyServlet', so
you can 

   http://....../yourwebapp/servlet/MyServlet

in the browser

2) you will need to extend VelocityServlet, so the <servlet-class> much
change to your class that extends VelocityServlet.

3) the properties params are right - put the velocity.properties file in
your webapp root (i.e  webapps/webappname/ ).

See examples/servlet_example2 for some ideas on working with properties.

geir



-- 
Geir Magnusson Jr.                           geirm@optonline.net
System and Software Consulting
Developing for the web?  See http://jakarta.apache.org/velocity/
You have a genius for suggesting things I've come a cropper with!

how to use custom velocity.properties

Posted by daljeetsingh <da...@softhome.net>.
Hello

I need to use a custom velocity.properties file for all the servlets in
a web application.

In order to do that I added the following to the web.xml
    <servlet >
      <servlet-name>org.apache.velocity.servlet.VelocityServlet</servlet-name>
      <servlet-class>org.apache.velocity.servlet.VelocityServlet</servlet-class>
      <init-param>
         <param-name>properties</param-name>
         <param-value>velocity.properties</param-value>
      </init-param>
    </servlet>
    
This change has not been sufficient for the velocity servlet to use the
custom properties file in the loadConfiguration method ??

What's wrong?

Thanks

Re: ReferenceException

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
Lane Sharman wrote:
> 
> Piero,
> 
> The velocity introspection engine, as you've read from Geir's
> post, requires getter and setter methods for public instance
> members.

Actually, no, that's not 100% correct.

We restrict access to only the public methods of a class, not just
'getter/setter', and follow a slightly extended version of the standard
Java bean spec for finding methods for you.  These methods do not have
to be getter/setter methods in the spirit of

get<Property>()
set<Property>( value )

although using them is very common (many tools know how to introspect
and let you manipulate beans), and very convenient. They give you a
degree of freedom in your design as they don't make any 'statement'
about implementation.  For example, suppose you wanted to have an object
that can supply a name.  You could do something like

public class Foo
{
   private String name;

   public String getName()
   {
      return name;
   }
}

and then access in your template like

$myfoo.name

Velocity will intropect the object named 'myfoo' in the context, and
look for method signatures like

public getName()
public getname()

and even

public get( String )

So therefore, to take advantage of the latter, you could also use *any*
class that implements a Map interface, including Hashtable, HashMap,
TreeMap, etc, or any class of your creation, and your template wouldn't
have to change when your data model changed.  

The reason we do this is mainly to help enforce good OO design and
encapsulation.  It is something I believe you should do anyway.  It
means that your classes are reusable in tools, JSPs, etc.

I do a bit of professional consulting using Velocity, and I agree that
sometimes you just want to slap something together - if you need a quick
and dirty class to hold stuff, use a Hashmap.  It works great.  Do your
references in your template like

$foo.bar

and then later, if you have to evolve that data containing class into
something more substantial, you won't have to change your template.

> 
> For classes whose role is to represent data, not behavior, or
> for which there are other good reasons (performance, ease of
> use), I suggest that this model is overly restrictive. The
> WebMacro introspection engine allows you to set/get public
> members directly.

I would be interested to see what the performance result is in a
real-world test case (and then compare to the cost of change later :)
 
> A class with public instance members have a role to play in
> applications, especially when the instance containing them is
> referenced by a secure and debugged referent.

And when you can demonstrate a provably secure and debugged referent, 
let me publish the paper with you...

In all seriousness, Lane is from the WebMacro community, and we get into
this kind of good-natured crossposting between lists all the time.
Actually, not enough, in my opinion ;)

Give WebMacro a look if you aren't 100% convinced that Velocity is the
solution for you - don't take our word for it.  After all, you will have
to live with the choice.  That said, I used to be a WebMacro user :)

geir

-- 
Geir Magnusson Jr.                           geirm@optonline.net
System and Software Consulting
Developing for the web?  See http://jakarta.apache.org/velocity/
You have a genius for suggesting things I've come a cropper with!

Re: ReferenceException

Posted by Jon Stevens <jo...@latchkey.com>.
on 7/2/01 4:47 PM, "Lane Sharman" <la...@san.rr.com> wrote:

> A class with public instance members have a role to play in
> applications, especially when the instance containing them is
> referenced by a secure and debugged referent.
> 
> -lane

public instance members which are not static (or static final) is bad OO
design IMHO and unlike WebMacro, Velocity does a very good job of not
encouraging people to go down the wrong path of bad design. :-)

It is easy enough to get around this design choice in Velocity by simply
using a Hashtable and a better design for the Hyperlink class...

public class Hyperlink
{
    private Hashtable innerTable = new Hashtable();

    public String get(String name)
    {
        return (String) innerTable.get(name);
    }
    public void set(String name, String value)
    {
        innerTable.put(name, value);
    }
}

Then, in your code:

    hl.set("Label", "/site/section");

And in your template:

$hl.Label

The advantage to this design is that you no longer have to modify the
Hyperlink class in order to add another "field", you don't have a bad OO
design in your class and the end result is very similar to what you already
have.

On top of it, you have the ability to add more control over the assignment
of the variables. For example, you could have the set() method throw an
exception if a name already exists or do data validation on the items going
into the fields. This is something you wouldn't be able to do with straight
assignment of instance variables.

Thanks,

-jon


Re: ReferenceException

Posted by Lane Sharman <la...@san.rr.com>.
Piero,

The velocity introspection engine, as you've read from Geir's
post, requires getter and setter methods for public instance
members.

For classes whose role is to represent data, not behavior, or
for which there are other good reasons (performance, ease of
use), I suggest that this model is overly restrictive. The
WebMacro introspection engine allows you to set/get public
members directly.

A class with public instance members have a role to play in
applications, especially when the instance containing them is
referenced by a secure and debugged referent.

-lane



piero de salvia wrote:

> Hi guys,
>
> It's one of those days when nothing works...
>
> I have:
>
> public class Hyperlink {
>
>         public String   Location;
>         public String   Label;
> }
>
> and in my servlet I do:
>
> Hyperlink hl = new Hyperlink();
> hl.Label = "/site/section";
> hl.Location = "Section";
> ctx.put("hl", hl);
>
> and in my template I do:
>
> <a href="$hl.Location">$hl.Label</a>
>
> and my result is:
>
> <a href="$hl.Location">$hl.Label</a>
>
> plus 2 ReferenceExceptions:
>
> Tue Jul 03 00:29:21 GMT+02:00 2001   [warn]
> org.apache.velocity.runtime.exception.ReferenceException:
> reference : template = /piergi/layout/navbar.vm [line
> 6,column 42] : $hl.Location is not a valid reference.
> Tue Jul 03 00:29:21 GMT+02:00 2001   [warn]
> org.apache.velocity.runtime.exception.ReferenceException:
> reference : template = /piergi/layout/navbar.vm [line
> 6,column 56] : $hl.Label is not a valid reference.
>
> Should I give up programming?
>
> piero de salvia
>
> __________________________________________________
> Do You Yahoo!?
> Get personalized email addresses from Yahoo! Mail
> http://personal.mail.yahoo.com/

--
Lane Sharman
Direct: 858-755-2868
http://corporate.acctiva.com/lane
Need Commercial Credit? http://www.acctiva.com
Evolving Computation:
http://www.javaworld.com/javaworld/jw-04-2001/jw-0406-ai.html