Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I install and use Apache Velocity?

I have installed Apache server 2.4, and Ant 1.8.3, I have downloaded Velocity 1.7, and Velocity tools 2.0. I have read the installation documentation for Apache Velocity about ten times, and Googled it for two days, I still have no idea what to do with these files. Can anyone provide a detailed description on how to install Velocity please?

I was hoping to develop using Eclipse, so I have also installed this.

I would appreciate any assistance as I am stuck.

Thank you.

Update:

I have configured Eclipse to use Tomcat and have worked my way through a tutorial and managed to get this all working, but I do not know how exactly to start a velocity project in Eclipse, if anyone has used Eclipse to build a Velocity project, I would appreciate some advice on how to set that up.

Thanks.

like image 954
deucalion0 Avatar asked Apr 23 '12 08:04

deucalion0


1 Answers

You will also need a Java EE container (aka servlet container) like Apache Tomcat. The Apache HTTP server doesn't know anything about Java. Please note that Tomcat can itself be used directly as an HTTP server - you would typically choose to keep Apache HTTP server in front of Tomcat on production servers to speed up static files requests among other reasons (Apache HTTP can forward requests to Tomcat with the mod_proxy_ajp module). But for the beginning, it's easier to directly use the HTTP service provided by Tomcat.

Then, you need to get familiar with the concept of a Web Application. It's nothing more than a specific hierarchy of files (that can be compressed into a jar file with the .war extension). For a web application using Velocity, that would typically be:

./ ← root of your web application
./index.vhtml ← your welcome page template
./foo/bar.vhtml ← any other file or subdirectory containing your web resources
./WEB-INF/ ← the WEB-INF directory contains all web application configuration
./WEB-INF/web.xml ← maps HTTP requests towards filters and servlets
./WEB-INF/tools.xml ← optional configuration file for your Velocity custom tools
./WEB-INF/velocity.properties ← optional file to tune Velocity configuration
./WEB-INF/lib/ ← contains all the libraries needed by your web application
./WEB-INF/src/ ← contains your custom Java classes source code
./WEB-INF/classes/ ← contains your custom Java classes

Your web.xml file has to map adequate requests towards the VelocityViewServlet. It will look like this:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

  <servlet>
    <servlet-name>view</servlet-name>
    <servlet-class>org.apache.velocity.tools.view.VelocityViewServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>view</servlet-name>
    <url-pattern>*.vhtml</url-pattern>
  </servlet-mapping>

</web-app>

I cannot help you much with Eclipse as I don't use it, but there seems to be several tutorials online if you search for tomcat + eclipse. Here's one that looks interesting:
http://www.coreservlets.com/Apache-Tomcat-Tutorial/tomcat-7-with-eclipse.html

like image 79
Claude Brisson Avatar answered Nov 09 '22 04:11

Claude Brisson