You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by John Rodriguez <jr...@cs.columbia.edu> on 2004/09/17 11:24:52 UTC

problem reading template file from velocityservlet

I am sure this is a noob question, but after trying many things from
newsgroups and web snippets, I have no other ideas.

I am trying to run an example from "Mastering Apache Velocity" where an
XML file is parsed and used in evaluating the template file.

However, in the code (see line of code below, commented for convenience),
displayxml.vm is not found by the class.  It is currently located in my
webapp root directory.  cds.xml IS found by the SAXBuilder object when
instantiated with the absolute pathname.

While investigating, I tried the common suggestion of setting the property
"file.resource.loader.path" to the location of the file, but the same
error occurs.  The "catalina.out" file says:

org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource 'displayxml.vm'
        at org.apache.velocity.runtime.resource.ResourceManagerImpl.loadResource(ResourceManagerImpl.java:458)
        at org.apache.velocity.runtime.resource.ResourceManagerImpl.getResource(ResourceManagerImpl.java:341)
        at org.apache.velocity.runtime.RuntimeInstance.getTemplate(RuntimeInstance.java:831)
	...

In fact I tried all three ways of changing properties, via a custom
Properties object, via a custom.properties file, and via
Velocity.setProperty()...same results.

I am not sure exactly what the "." in "file.resource.loader.path" refers
to, however, I believe it's ${CATALINA_HOME}/bin based on an earlier bug I
had with symlinks to jar files.  I've temporarily sidestepped that problem
by copying all jars currently needed to my webapp's /lib directory

I'm currently using Apache Tomcat/4.1.30, JVM version 1.4.2_05-b04,
OS 2.4.20-gentoo-r2, Velocity 1.4

Any ideas?

/************ ATTACHED CODE ***************/

import javax.servlet.http.*;
import org.apache.velocity.app.*;
import org.apache.velocity.*;
import org.apache.velocity.context.*;
import org.apache.velocity.servlet.*;
import org.apache.velocity.exception.*;
import org.apache.xerces.parsers.*;
import org.jdom.*;
import org.jdom.input.*;

public class VelocityServletExample extends VelocityServlet
{
  public Template handleRequest( HttpServletRequest request, HttpServletResponse response, Context context)
  {
     try
     {
       Velocity.init();

       SAXBuilder builder = new SAXBuilder("org.apache.xerces.parsers.SAXParser");
       Document root = builder.build("....../cds.xml");  /* path deliberately omitted */

       context.put("root", root);
     }
     catch(Exception e) { try { e.printStackTrace(response.getWriter()); } catch(Exception f){} }

     Template template = null;

     try
     {
       template = Velocity.getTemplate("displayxml.vm");  /*** problem ***/
     }
     catch(Exception e){ e.printStackTrace(); }

     return template;
  }
}

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org