You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Jeff Beekman <je...@veritas.com> on 2001/03/29 18:09:52 UTC

Problem with resource path

Hello All,

I've been through the archives, and found some things that look like they
may be aspects of this problem (which may be a bug, but is probably a
misconfiguration). I'm new to Java, Tomcat and Velocity, so it is probably
my error.

Simply put, Velocity is trying to find templates using what appears to be
the base JVM context, e.g., when I try to run the servlet_example1 include
in the Vel distribution, i get a FileResourceNotFound exception and the path
D:\opt\jakarta-tomcat-4.0-b1\bin\.\sample.vm.

Why is Velocity using this path rather than the webapps context? Let me know
if there's any configuration information I can post that will help. I
suspect it is a simple problem. Those always get me... =)

TIA
_______________
Jeff Beekman
Programmer/Analyst - Advanced Support Technologies
VERITAS Software
407.531.7534

Re: Problem with resource path

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
Jon Stevens wrote:
> 
> on 3/29/01 11:10 AM, "Art Gillespie" <ag...@i-no.com> wrote:
> 
> > That's correct. Velocity uses the directory from which
> > Tomcat was launched.  I've been using ../webapps/myapp in
> > the file.resource.loader.path property or even the
> > full path.  TTBOM
> >
> > Art Gillespie
> 
> Actually, it shouldn't use the directory where Tomcat was launched. If it
> is, then that is a bug.

Of course it's the directory from where Tomcat was launched if you
happen to use ".", the current directory, as your resource path, which
is the default.

I will answer this off of the original post.

geir

-- 
Geir Magnusson Jr.                               geirm@optonline.net
Developing for the web?  See http://jakarta.apache.org/velocity/

Re: Problem with resource path

Posted by Jon Stevens <jo...@latchkey.com>.
on 3/29/01 11:10 AM, "Art Gillespie" <ag...@i-no.com> wrote:

> That's correct. Velocity uses the directory from which
> Tomcat was launched.  I've been using ../webapps/myapp in
> the file.resource.loader.path property or even the
> full path.  TTBOM
> 
> Art Gillespie

Actually, it shouldn't use the directory where Tomcat was launched. If it
is, then that is a bug.

-jon

-- 
If you come from a Perl or PHP background, JSP is a way to take
your pain to new levels. --Anonymous
<http://jakarta.apache.org/velocity/ymtd/ymtd.html>


Re: Problem with resource path

Posted by Art Gillespie <ag...@i-no.com>.
That's correct. Velocity uses the directory from which
Tomcat was launched.  I've been using ../webapps/myapp in
the file.resource.loader.path property or even the
full path.  TTBOM

Art Gillespie



On Thu, Mar 29, 2001 at 11:09:52AM -0500, Jeff Beekman wrote:
> Hello All,
> 
> I've been through the archives, and found some things that look like they
> may be aspects of this problem (which may be a bug, but is probably a
> misconfiguration). I'm new to Java, Tomcat and Velocity, so it is probably
> my error.
> 
> Simply put, Velocity is trying to find templates using what appears to be
> the base JVM context, e.g., when I try to run the servlet_example1 include
> in the Vel distribution, i get a FileResourceNotFound exception and the path
> D:\opt\jakarta-tomcat-4.0-b1\bin\.\sample.vm.
> 
> Why is Velocity using this path rather than the webapps context? Let me know
> if there's any configuration information I can post that will help. I
> suspect it is a simple problem. Those always get me... =)
> 
> TIA
> _______________
> Jeff Beekman
> Programmer/Analyst - Advanced Support Technologies
> VERITAS Software
> 407.531.7534
---end quoted text---

Re: Problem with resource path

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
Jeff Beekman wrote:
> 
> Hello All,
> 
> I've been through the archives, and found some things that look like they
> may be aspects of this problem (which may be a bug, but is probably a
> misconfiguration). I'm new to Java, Tomcat and Velocity, so it is probably
> my error.
> 
> Simply put, Velocity is trying to find templates using what appears to be
> the base JVM context, e.g., when I try to run the servlet_example1 include
> in the Vel distribution, i get a FileResourceNotFound exception and the path
> D:\opt\jakarta-tomcat-4.0-b1\bin\.\sample.vm.
> 
> Why is Velocity using this path rather than the webapps context? Let me know
> if there's any configuration information I can post that will help. I
> suspect it is a simple problem. Those always get me... =)

Briefly, the first thing to remember is that Velocity is a general
purpose template engine, and 'webapp' is just something you run across
in it's most popular use, Servlets,  but not the only use.

Velocity has configurable resource loaders for getting templates.  The
default configuration is to use the FileResourceLoader as the loader,
and it is configured by default to use ".", the current directory, as
the 'root' of the template tree.  Any reference to a tempate will be
relative to this.

Now, for webapps, this is an issue, as you have discovered.  Here are
the solutions that I and others use :

1) For anything based on VelocityServlet, override the method
loadConfiguration(ServletConfig config ) and setup your resource paths
in there.  This is why loadConfiguration() was added.  Take a look at
the source to see what it is doing re loading the properties file, and
do something similar then altering the path from relative to absolute at
runtime based on getRealPath() in ServletContext (from ServletConfig).

As an example, if you are using a properties file will have a line like
:

  file.resource.loader.path = <your template path(s) relative to your
webapp root>

so as the default impl of loadConfiguration() does, load the Properties
file and then iterate through looking for the key
'file.resource.loader.path'

then just do something like.

  prop.setProperty( "file.resource.loader.path", 
     config.getServletContext().getRealPath( 
     prop.getProperty( "file.resource.loader.path") ));

modulo my usual "i'm just winging it here..." errors.

I use this technique myself in stuff I do for clients, and it makes
things very portable.


2) A hipper way is to simply bundle up your templates into a jar, drop
that jar into WEB-INF/lib, and then configure Velocity to use the
ClasspathResourceLoader.  Then you don't care - the classloader will be
providing the resources, and then all paths are the path in the jar.

geir

-- 
Geir Magnusson Jr.                               geirm@optonline.net
Developing for the web?  See http://jakarta.apache.org/velocity/