velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Geir Magnusson Jr. <>
Subject Re: Newbie - Keep getting ResourceNotFoundException
Date Wed, 27 Nov 2002 17:17:47 GMT
If you already solve the problem, ignore - my scan of the list didn't 
seem to have any help for you...

On Tuesday, November 26, 2002, at 02:36 PM, Søren Neigaard wrote:

> I'm trying to make a simple HelloWorld example, and I'm doing this
> from inside the doGet method in a servlet:
> ------------------
> Velocity.init();
> VelocityContext context = new VelocityContext();
> context.put("name", new String("Velocity Test"));
> Template template = null;
> try {
>   template = Velocity.getTemplate("mytemplate.vm");
>   template.merge(context,response.getWriter());
> }
> catch(Exception e){
>   e.printStackTrace();
> }
> ------------------
> Where does it look for the template? Can it be on the file system, or
> must it be accessible via HTTP? I have now tried to pint it directly
> to the path where the file is, and i get this error:
> ------------------
> org.apache.velocity.exception.ResourceNotFoundException: Unable to
> find resource
> '/C:/jakarta-tomcat-4.1.10-LE-jdk14/webapps/yaf/mytemplate.vm'
> ------------------
> And the file is there!? Please advice, what is my (simple) problem?

It's a simple problem.

First, some comments :

1) you don't want to put init() in the doGet().  First, it's not going 
to work after the first time, and two, it's much better to put this 
into the servlets' init method, so it will get called at the right time 
in the servlet life cycle.

2) There is nothing wrong with rolling your own servlet - it's what I 
do and can customize things the way I want them to be.  VelocityServlet 
is a nice base, and it has a nice model to work with, but there are 
many ways to do it.

3) The problem - the problem is that by default, velocity's resource 
loader is the FileResourceLoader, and further, it defaults to the 
'current directory', whatever that is, when init()-ed w/o parameters.  
The solution is really simple - you want to set the file resource 
loader path with the right thing - see how VelocityServlet does it, or 
the servlet examples in the distro.

Other solutions is to use the webapp resource loader from the tools 
project as well, as that is meant to not depend on the filesystem, 
which is the better thing to do in a servlet environment.


Geir Magnusson Jr                                   203-355-2219(w)
Adeptra, Inc.                                       203-247-1713(m)

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

View raw message