You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by julia schmitz <ju...@web.de> on 2006/04/25 08:38:16 UTC

Velocity Template ResourceNotFoundException


Hello

When i run my Servlet with velocity i get this Error:

Error: org.apache.velocity.exception.ResourceNotFoundException

I tried many ways discuss here, but nothing works. 

I use Eclipse 3.1.2 , Tomcat 5.0.28 (Windows Executable) and Velocity velocity-dep-1.4.jar.

The app_root is in the workspace of Eclipse, i edit the wep.xml and the velocity.properties, but it does not work.

web.xml 

 <init-param>
 <param-name>properties</param-name>
 <param-value>>WEB-INF/velocity.properties</param-value>
 </init-param>

file.resource.loader.path = /
file.resource.loader.cache = true
file.resource.loader.modificationCheckInterval = 1
runtime.log.error.stacktrace = true
runtime.log = /WEB-INF/velocity.log

I will be happy if someone have an idea where the problem is. 

Thank you very much

Julia Schmitz


	
SMS schreiben mit WEB.DE FreeMail - einfach, schnell und 
kostenguenstig. Jetzt gleich testen! *http://f.web.de/?mc=021192* [http://f.web.de/?mc=021192] 

Re: Velocity Template ResourceNotFoundException

Posted by is...@setgame.com.
Since no one else is repling, i will tell you how i do it.
It is most diffently wrong by any standard, but it does work quite well
for us, and has over many different situation including

develpment in eclipse.
deployed in a war.
deployed in a unpacked war.
unit tests via ant & cruise control.

it's a bit complex, so i'll layout the basics.
I will send the files i use if you want them, but i'm pretty sure the list
server strips attachments off, so i'll do it by request.

first, we create a convience function to setup the parameters.
then when you want to parse a template. call it and pass a context aware
as a call back.

lets look at the setup. you will still need some sort of configuration,
for us, it's lumped with all are other configurations for the purpose of a
single file. but for here i'm going to hard code.
there is a natural refactoring here that creates to functions.
the very generic.
 public static Writer parse(String template, Properties props,
ContextAware process[], Writer out)

the semi generic
  public static String parse(String template, Properties props,
ContextAware[] process)

and of course the project specific. which is what you would write.
again, since we are either reading from jar / file, here's mine.

  /***********************************************************************/
  public static String parseJarFile(String template, ContextAware process)
  {
    Properties props = getProperties();
    return VelocityParser.parse(template, props, new
ContextAware[]{process, Default.INSTANCE});
  }
  /***********************************************************************/
  private static Properties getProperties()
  {
    Properties props = new Properties();
    if (Config.FORCE_ACTUAL_FILE_INSTEAD_OF_JAR)
    {
      props.put("resource.loader", "file");
      props.put("file.resource.loader.path", getPath());
      props.put("file.resource.cache", "" + true);
    }
    else
    {
      props.put("resource.loader", "class");
      props.put("class.resource.loader.description", "Velocity
ServletContext Resource Loader");
      props.put("class.resource.loader.class",
"com.spun.util.velocity.ServletContextLoader");
      props.put("class.resource.cache", "" + true);
      ServletContextLoader.registerServletContext(BasicCompanyServlet.getContext());
      props.put("class.resource.loader.path",
"/WEB-INF/resources/templates/" + ", " +"/");

    }
    props.put("runtime.introspector.uberspect",
"com.spun.util.velocity.TestableUberspect");
    props.put("velocimacro.context.localscope", "" + true);
    props.put("velocimacro.permissions.allow.inline.local.scope", "" + true);
    return props;
  }
  /***********************************************************************/
  private static String getPath()
  {
    if (path == null)
    {
      path = Config.ALTERNATIVE_TEMPLATE_DIRECTORY + ", " +
Config.TEMPLATE_DIRECTORY + ", " +
convertDirectory(Config.getAlternativeHtmlDirectory())
          + ", " + convertDirectory(Config.getHtmlDirectory());
      My_System.variable("path", path);
    }
    return path;
  }
  /***********************************************************************/

as i understand your situation, you would just want the file reader part
of this.

the last part is to write a class that can be parsed. i'll make and
example here.

class Family implement ContextAware
{
Person mom, dad, brother, sister;

String getFamilyTreeAsHtml()
{
 VelocityParser.parse("template.htm", this);
}

public void setupContext(Context context)
{
context.put("mom",mom);
context.put("dad",dad);
context.put("sister",sister);
context.put("brother",brother);
}
}


anyways, this works well for us, has the distinct advantage, of being able
to break/log/troubleshoot the different settings you are passing in. if
you want more just ask.

llewellyn






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