I'm currently trying to work my way through the simple velocity tutorial at
http://www.javaranch.com/journal/2004/03/Velocity-AnIntroduction.html and cannot get velocity to see the "hello.vm" template file when the statement template=Velocity.getTemplate("hello.vm") is executed in the "HelloTest.java"
servlet listed on page three of the tutorial. Tomcat 6 seems to startup ok with no errors or exceptions, but when I actually try to access my servlet I get this warning:
WARN main org.apache.commons.digester.Digester - [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:velocity' did not find a matching property.
Not sure if the warning is important at this point because I get it both when I startup Tomcat standalone or via eclipse. Later I see that the /WEN-INF/velocity.properties is being looked for like this:
15:42:09,671 INFO http-8282-1 org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/velocity] - Velocity [debug] VelocityViewServlet: Looking for custom properties at '/WEB-INF/velocity.properties'
15:42:09,671 INFO http-8282-1 org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/velocity] - Velocity [debug] VelocityViewServlet: No custom properties found. Using default Velocity configuration.
and I purposely did not specify one for startup, because I wanted to specify some properties dynamically in my servlet. Next I see where the velocimacro library is being looked for:
Velocimacro : initialization starting.
15:42:09,734 INFO http-8282-1 org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/velocity] - Velocity [debug] Velocimacro : "velocimacro.library" is not set. Trying default library: VM_global_library.vm
15:42:09,734 INFO http-8282-1 org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/velocity] - Velocity [debug] Velocimacro : Default library not found.
its apparently not found, even though I have had it both immediately under /WEB-INF as well as in /WEB-INF/templates where I've also seen that its also possible to put it, and it seems like sometimes it finds it and sometimes it doesn't. Next the toolbox.xml specified in my web.xml isn't found either though it is defined in the web.xml:
RuntimeInstance successfully initialized.
15:42:09,734 INFO http-8282-1 org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/velocity] - Velocity [debug] VelocityViewServlet: No toolbox entry in configuration. Looking for '/WEB-INF/toolbox.xml'
15:42:09,781 INFO http-8282-1 org.apache.velocity.tools.view.servlet.ServletToolboxManager - Using config file '/WEB-INF/toolbox.xml'
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>velocity</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>velocity</servlet-name>
<servlet-class>
org.apache.velocity.tools.view.servlet.VelocityViewServlet
</servlet-class>
<init-param>
<param-name>org.apache.velocity.toolbox</param-name>
<param-value>/WEB-INF/toolbox.xml</param-value>
</init-param>
<load-on-startup>10</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>velocity</servlet-name>
<url-pattern>*.vm</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>HelloTest</display-name>
<servlet-name>HelloTest</servlet-name>
<servlet-class>servlets.HelloTest</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloTest</servlet-name>
<url-pattern>/HelloTest</url-pattern>
</servlet-mapping>
</web-app>
once it reaches this point in the process then I start getting exceptions which look like they're related to some of the tools specified in the toolbox like this:
ERROR http-8282-1 org.apache.velocity.tools.view.ViewToolInfo - Exception when calling init(Object) on org.apache.velocity.tools.struts.MessageTool@15dd910
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
ERROR http-8282-1 org.apache.velocity.tools.view.ViewToolInfo - Exception when calling init(Object) on org.apache.velocity.tools.struts.ActionMessagesTool@1ae90c
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
ERROR http-8282-1 org.apache.velocity.tools.view.ViewToolInfo - Exception when calling init(Object) on org.apache.velocity.tools.struts.ErrorsTool@ba4211
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source
ERROR http-8282-1 org.apache.velocity.tools.view.ViewToolInfo - Exception when calling init(Object) on org.apache.velocity.tools.struts.ValidatorTool@47a0d4
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
and the process finally finishes up running like this:
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
at org.apache.velocity.tools.struts.ValidatorTool.init(ValidatorTool.java:188)
... 23 more
15:42:09,859 INFO http-8282-1 servlets.HelloTest - Beginning HelloTest
15:42:09,859 INFO http-8282-1 servlets.HelloTest - returning from HelloTest
15:42:09,859 INFO http-8282-1 org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/velocity] - Velocity [warn] VelocityViewServlet: couldn't find template to match request.
I would appreciate any help on figuring this out. Let me know if anymore information is needed. I tried as much as possible to do everything mentioned in the setup or steps mentioned in this tutorial. More of my files are show below:
HelloTest.java:
package servlets;
import java.util.Properties;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.velocity.Template;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.context.Context;
import org.apache.velocity.tools.view.servlet.VelocityViewServlet;
public class HelloTest extends VelocityViewServlet {
/**
*
*/
private static final long serialVersionUID = -8731237619567372345L;
public Template handleRequest( HttpServletRequest request,
HttpServletResponse response,
Context context ) {
Logger logger = Logger.getLogger(HelloTest.class);
logger.info("Beginning HelloTest");
Properties p = new Properties();
p.setProperty( "resource.loader", "class" );
p.setProperty( "class.resource.loader.class",
"org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader" );
p.setProperty( "class.resource.loader.path",
"/WEB-INF/templates");
//"C:/Tomcat/apache-tomcat-6.0.26/webapps/velocity/WEB-INF/templates");
p.setProperty( "class.resource.loader.cache", "true");
p.setProperty( "class.resource.loader.modificationCheckInterval","5");
try{
Velocity.init(p);
}
catch(Exception e){
System.err.println("Velocity Engine Start Exception: " + e.getMessage());
}
Template template = null;
try {
context.put("name", "Velocity Test");
template = Velocity.getTemplate("hello.vm");
} catch( Exception e ) {
System.err.println("Exception: " + e.getMessage() + e.getStackTrace());
}
logger.info("returning from HelloTest");
return template;
}
}
toolbox.xml:
<?xml version="1.0" encoding="UTF-8"?>
<toolbox>
<tool>
<key>link</key>
<scope>request</scope>
<class>org.apache.velocity.tools.struts.StrutsLinkTool</class>
</tool>
<tool>
<key>text</key>
<scope>request</scope>
<class>org.apache.velocity.tools.struts.MessageTool</class>
</tool>
<tool>
<key>messages</key>
<scope>request</scope>
<class>org.apache.velocity.tools.struts.ActionMessagesTool</class>
</tool>
<tool>
<key>errors</key>
<scope>request</scope>
<class>org.apache.velocity.tools.struts.ErrorsTool</class>
</tool>
<tool>
<key>form</key>
<scope>request</scope>
<class>org.apache.velocity.tools.struts.FormTool</class>
</tool>
<tool>
<key>validator</key>
<scope>request</scope>
<class>org.apache.velocity.tools.struts.ValidatorTool</class>
</tool>
</toolbox>
hello.vm:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>hello</title>
</head>
<body>
Hello $name
</body>
</html>
VM_global_library.vm:
#macro(onecolumnrow)
<tr><td></td></tr>
#end
My environment is as follows:
Eclipse Galileo
Tomcat 6
Velocity 1.6.3
Log4j-1.2.12
Then when I enter my url "http://localhost:8282/velocity/HelloTest".