2. What is Velocity?

Velocity is a Java-based template engine. It permits web page designers to reference methods defined in Java code. Template developers can work in parallel with Java programmers to develop web sites according to the Model-View-Controller (MVC) model, meaning that they can focus solely on creating templates, and programmers can focus solely on writing Java code. Velocity separates Java code from the template pages, making a project more maintainable over the long run.

In a web context, Velocit provides a viable alternative to Java Server Pages (JSPs) or PHP.

Velocity can be used to generate web pages, Java source code, SQL, PostScript and other output from templates. It can be used either as a standalone utility for generating source code and reports, or as an integrated component of other systems.

Velocity provides template services for a number of web frameworks, e.g. the Turbine web application framework. In this contect, Velocity offers a template service that will allow web applications to be developed according to a true MVC model.

2.1 The Fruit Store Example

Suppose you are a page designer for an online store that specializes in selling fruits. Let's call it The Online Fruit Store. Customers place orders for various types and quantities of fruits. They login to your site using their username and password, which allows them to view their orders and buy more fruits. Right now, apples on sale, which are very popular. A minority of your customers regularly buy mangos, which are also on sale, though not as popular and usually relegated to the margin of your web page. Information about each customer is tracked in your database, so one day the question arises: Why not use Velocity to target special deals on fruit to the customers who are most interested in those types of fruits?

Velocity makes it easy to customize web pages to your online visitors. As a web site designer at The Online Fruit Store, you want to make the web page that the customer will see after logging into your site.

You meet with software engineers at your company, and everyone has agreed that $customer will hold information pertaining to the customer currently logged in, that $fruitSpecial will be all the types of fruit on sale at present. The $promoTool object contains methods that help with promotion. For the task at hand, let's concern ourselves only with these three references. Remember, you don't need to worry about how the software engineers extract the necessary information from the database, you just need to know that it works. This lets you get on with your job, and lets the software engineers get on with theirs.

You could embed the following VTL statement in the web page:

<html><body>
Hello $customer.Name!
<table>
  #foreach( $fruit in $fruitSpecial )
    #if ( $customer.hasPurchased($fruit) )
      <tr>
        <td>
          $promoTool.getPromo($fruit)
        </td>
      </tr>
    #end
  #end
</table>
</body></html>

The exact details of the #foreach and #if statement will be described in greater depth in the Directives chapter; what's important is the impact this short script may have on your web site. When a customer with a penchant for mangos logs in, and mangos are on sale, that is what this customer will see, prominently displayed. If another customer with a long history of apple purchases logs in, the notice of an apple sale will be front and center. The flexibility of Velocity is enormous and limited only by your creativity.

This manual should help you get started with Velocity and the Velocity Template Language. It has a companion manual, The Velocity Developers Guide, which describes how to integrate Velocity into your own web applications (this is what the software engineers at your company want to read).