What is Velocity?
Velocity is a Java-based template engine. It permits web page designers to reference methods defined in Java code. At the Jakarta Velocity Project Website, where you can download the freely available source code, a thriving and growing community of users is ready to answer questions and offer solutions to common templating problems. Jakarta Velocity Project Website, offers the freely available code, and also provide a technical support community that how to use that code.
Why should I use it?
Velocity provides functionality to designed and
use general templating tools, so that it can work with any Java application
that requires data formatting and presentation as well.
Some reasons to use velocity :
- It adapts to many application areas
- It provides a simple and clear syntax for the template designer
- It offers a simple programming model for the developer
- With velocity you can develop and maintain template and code separately.
- The Velocity engine provides easy integration with any Java application environment.
- Velocity enables templates to access any public method of data objects in the context
Velocity Template Language (VTL):
The Velocity Template Language (VTL) provides the
easiest and simplest way to show and process dynamic content in web
page. Velocity use references to add dynamic content in web page,
variables are one type of reference that can use as a reference to refer
something in java code, or it can get its value from a VTL statement.
Here is an example of a VTL statement that can be embedded in an HTML document:
#set( $name = "Komal" )
|
This VTL statement begins with the # character and contains a directive: set. When a client requests your web page, the Velocity Templating Engine will search through your web page to find all # characters, then determine which mark the beginning of VTL statements, and which of the # characters that have nothing to do with VTL.
The # character is followed by a directive, set. The set directive used an expression (enclosed in brackets) - an equation that assigns a value to a variable. The variable is list on the left hand side and its value on the right hand side, these two are separated by an = character.
References
There are three types of references in the VTL: variables, properties and methods. As a programers using the VTL, The engineers must have an agreement on the references so you can use them correctly in your templates for proper functionality.
Variables
Velocity uses variable declaration with '$' sign.
Rules to declare variables:
A VTL Identifier must start with an alphabetic character (a .. z or A .. Z). The rest of the characters are limited to the following types of characters:
- alphabetic (a .. z, A .. Z)
- numeric (0 .. 9)
- hyphen ("-")
- underscore ("_")
Here are some examples of valid variable references in the VTL:
|
When VTL references a variable, such as $name, the variable can gets its value from either a set directive in the template, or from the Java code. For example, if the Java variable $name has the value bar at the time the template is requested, bar replaces all instances of $name on the web page. Alternatively, if I include the statement
|
The output will be the same for all instances of $name that follow this directive.
Properties
Second reference of VTL is properties, and properties have a
different format. To create properties in velocity uses '$' character followed a VTL Identifier, followed by a dot character (".") and another VTL
Identifier and so on.
These are some examples of valid property references in the VTL:
|
Methods
A method is use to do some task and defined in java code. Methods are references that consists of a leading "$" character followed a VTL Identifier. These are examples of valid method references in the VTL:
|
This notation is used for the following Methods
|
We might expect these methods to return the names of person from variable 'person' : 2
|
Conditionals 3
If / ElseIf / Else
The #if directive in Velocity allows for text to be included when the web page is generated, on the conditional that the if statement is true. For example:
|
An #elseif or #else element can be used with an #if element. 5
|
Logical AND operators
|
Logical OR operators
|
Logical NOT operators
|
Loops
Foreach Loop 9
The #foreach element allows for looping. For example:
|
Produces the following output:
|
Example 2
Stu.vm
|
Stu.java
|
Output 3
No of student is : 2 |