Java Tutorial - Java Script : Velocity

Java Tutorial - Java Script :

Velocity

When discussing template engines, there’s no way we could forget to discuss the Jakarta-Apache group’s contribution: Velocity. Velocity is a Java-based template engine similar to the previous templates engines we have examined; in fact its concept came from WebMacro. Also, like Tea and WebMacro, Velocity enforces the separation of the presentation and data-acquisition layers. Velocity provides some truly stellar capabilities, including the ability to generate SQL, PostScript and XML, and also serves as the template engine for the Turbine development framework. Velocity can also be very useful outside the Web application development arena. The summary information for Velocity is shown in the following table.We will install Velocity version 1.3 within the same Tomcat servlet container we used for the Tea and WebMacro demonstrations. The prerequisites for building the Velocity environment are Ant version 1.3 or later and the Java 2 SDK. The installation process involves the following steps:

Uncompress the Velocity archive to a work directory.

Change to the build directory and execute ./ant. This will build the Velocity executable .jar file and place it into the /bin directory.

CROSS-REFERENCE
Velocity comes with two .jar files in the distribution: one that contains the core Velocity classes without any external dependency classes (they are included but in separate jars) and one that contains the core classes along with the external dependency classes. This gives developers flexibility in using various versions of the collections classes and other dependent classes without having to deal with collisions.

 After Velocity has been compiled, perform the following steps:

1. Create the following directory structure in your servlet container:
TOMCAT_ROOT/webapps/velocity
TOMCAT_ROOT /webapps/velocity/WEB-INF/classes
TOMCAT_ROOT /webapps/velocity/WEB-INF/lib

2.Place the Velocity jar into the TOMCAT_ROOT/webapps/velocity/ WEB-INF/lib directory.

  Because the default dependency classes were fine for our purpose, we used the velocity-dep-1.2.jar that came with the distribution.

Switch back to your working directory and compile the examples that came with this distribution by going into the \build directory and executing the ant examples command.

Test your Velocity installation by installing the SampleServlet application that came with the distribution by copying SampleServlet .class to TOMCAT_ROOT \webapps\freeJ2EE\WEB-INF\classes, and sample.vm to the TOMCAT_ROOT \webapps\freeJ2EE directory.

 Create the following web.xml file, and place it into the WEB-INF directory.


<?xml version=”1.0” encoding=”UTF-8”?>

<!DOCTYPE web-app PUBLIC
“-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN”
“http://java.sun.com/dtd/web-app_2_3.dtd”>

<web-app>
<servlet>
<servlet-name>Servlet_SampleServlet</servlet-name>
<servlet-class>SampleServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Servlet_SampleServlet</servlet-name>
<url-pattern>/servlet/SampleServlet</url-pattern>
</servlet-mapping>
</web-app>

Restart Tomcat to allow it to register the new application.

After you’ve restarted Tomcat, test the Velocity installation by executing the following URL from your browser:


If everything goes as expected, you should see the screen shown in Figure 6.9.

 In keeping with the format for presenting the previous template engines, we will show you how to develop and deploy the HelloMyCity application as we did previously. First, we will develop our template file, HelloMyCity.vm.
<html>
<head><title>Hello My City</title></head>
<body>

<h1>Hello $myCity!</h1>
</body>
</html>