Apache Velocity error in Eclipse [Java, Eclipse Luna, Velocity 2.0, VelocityView, Tomcat 7]
I want to start by defining that I am 90% new to everything Java, Eclipse, templating engines mean.
In my attempt to create a speed template that will work as intended, I have used Eclipse Luna (EE), Tomcat 7, Apache Velocity1.7, Velocity Tools 2.0, VelocityToolsView 2.0
The PROBLEM is that when I start Tomcat from Eclipse (if possible, run the project) and go to "localhost: 8080 / VelocityTemplateHomework4 / index.vm" it just gives me a whole page with the error "HTTP Status 500 - Failed to instantiate class servlet org.apache.velocity.tools.view.servlet.VelocityViewServlet "
In Eclipse (EE), I basically followed this tutorial :
http://thegeekhead.blogspot.ro/2009/06/how-to-configure-eclipse-tomact-55.html
Anyway, I will post my code, but please check the link (maybe there is a problem there as it is outdated)
SimpleServlet.java
package myPackage;
public class SimpleServlet {
private String message = "Hello Damn World!!!";
public String getMessage() {
return message;
}
public void setMessage(String m) {
message = m;
}
/** To test exception handling in templates. */
public boolean whine() {
throw new IllegalArgumentException();
}
}
web.xml
<?xml version='1.0' encoding='utf-8'?>
<web-app>
<!-- Define Velocity template compiler -->
<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>
<init-param>
<param-name>org.apache.velocity.properties</param-name>
<param-value>/WEB-INF/velocity.properties</param-value>
</init-param>
</servlet>
<!-- Map *.vm files to Velocity -->
<servlet-mapping>
<servlet-name>velocity</servlet-name>
<url-pattern>*.vm</url-pattern>
</servlet-mapping>
</web-app>
toolbox.xml
<?xml version="1.0"?>
<toolbox>
<xhtml>true</xhtml>
<tool>
<key>serv</key>
<scope>request</scope>
<request-path>index.vm</request-path>
<class>SimpleServlet</class>
</tool>
<data type="number">
<key>version</key>
<value>1.1</value>
</data>
<data type="boolean">
<key>isSimple</key>
<value>true</value>
</data>
<data type="string">
<key>foo</key>
<value>this is foo.</value>
</data>
<data type="string">
<key>bar</key>
<value>this is bar from velocity.</value>
</data>
<tool>
<key>map</key>
<scope>session</scope>
<class>java.util.HashMap</class>
</tool>
<tool>
<key>date</key>
<scope>application</scope>
<class>org.apache.velocity.tools.generic.DateTool</class>
</tool>
</toolbox>
velocity.properties
webapp.resource.loader.path=/WEB-INF/templates/
... and a simple index.vm (which, for example, if you noticed on the tutorial site, contains an HTML closing closing tag)
<html>
<body>
<h2>My text is : $serv.getMessage() </h2>
</body>
</html>
File structure on the left:
Sorry for the long post, any help would be appreciated!
source to share