Wednesday 12 December 2012

Velocity Template Tutorial_2


SCRIPTING LANGUAGE :
  • It is a programming language where the program or script is directly interpreted or executed instead of compiling & then executing. Example: Javascript.
VELOCITY-TEMPLATE LANGUAGE (VTL) :

  • It is a scripting language used inside the velocity template file for generating dynamic contents.
  • It is a case sensitive language.
  • It is white space sensitive language meaning '#end' is different from “# end”[Note: Space between “#” and “end”]
  • In VTL we can specify three types of comments as like JAVA. They are :
    1.Single-line comment (##)
      Example: ## This is commented line 

    2.Multi-line comment(#*......*#)
      Example: #* The below lines will not be   processed by the velocity engine because this is multi-line command *# 

    3.Documentary comment(#**.......*#)
      Example : #** The below lines will not be processed by the velocity engine because this is multi-line command *#
  • VTL has two parts DIRECTIVES and REFERENCES.
     DIRECTIVES => Used for controlling and doing action. Controlling like if,else ,else if control statement.Doing like setting variables and creating functions

     REFERENCES => Used for accessing objects and their methods.[NOTE : Directives always begin with “#” while references always beging with “$”]
 
REFERENCES :

  • Always start with “$” character
Types of References : 
Variables :Used to declare variables in template-file
 
Properties:Used to access methods in objects using accessor and mutator methods like JAVABEAN accessing
 
Methods : Used to access methods in objects.


VARIABLE REFERENCES :
  • Its sames as java variables.
  • Always start with “$” symbol.
  • When declaring variable reference in template file it should start with alpha-numeric character.
  • Can have underscore(_) and hypen(-) in variable reference.
  • Syntax
      $variableName
      EXAMPLE : #set($userName = “vignesh”)
      EXAMPLE 2: $userName ## userName value is in java-code. 

  • Variable references can get value from #set directive or java-code.

PROGRAMMING :
-->

REQUIRED PACKAGES :
  • 1.velocity-1.6.2.jar
  • 2.commons-collections-3.2.1.jar
  • 3.commons-lang-2.4.jar
  • 4.oro-2.0.8.jar
TO Download this files Click here

JAVA-FILE:
import java.io.StringWriter;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;

public class HelloWorld {
public static void main(String[] args) throws Exception {

VelocityEngine velocityEngine = new VelocityEngine();
velocityEngine.init();
Template template = velocityEngine.getTemplate("./src/helloworld.txt");

VelocityContext context = new VelocityContext();
context.put("password","vignesh"); // =====>STEP:1

StringWriter writer = new StringWriter();
template.merge(context, writer);
System.out.println(writer);
}
}

helloworld.txt-FILE

#set($name ="vignesh") ## =====>STEP:2

Hello $name! Welcome to Velocity! ## =====>STEP:3

Password ---> $password ## =====>STEP:4
OUTPUT
Hello vignesh! Welcome to Velocity!

Password ---> vignesh
EXAPLANATION :
STEP: 1 ===> putting the key-value pair into velocity context.put
STEP: 2 ===> Declaring the reference variable.
STEP: 3 ===> Variable reference “$name” referring the value from #set directive.vm
STEP: 4 ===> Variable reference “$password” referring the value from java code which is set in STEP 1.


PROPERTIE REFERENCES:
  • Used to access methods in java objects.
  • Used only to access or call only “getter” methods in JAVABEAN , we cannot use “setter” here.
  • Can refer to velocityContext key value or JAVABEAN getter methods.
  • SYNATX :
    $variableName.methods
    EXAMPLE:$customer.Address
    //Here $customer.Address can also be referring to a //method of java-bean or key in VelocityContext object. 

  • VelocityEngine find wheather it referes to key in VelocityContext object or method in java bean object and return the appropriate value.
METHOD REFERENCES:
  • Used to call methods in java objects.
  • Can be used to call setter methods or any methods in java-object.
  • Can be used to call java.array.List class methods also like $listObject.get(0);

  • SYNATX :
    $variableName.methodName(parametersForMethods)
     EXAMPLE: $customer.getAddress()
              $customer.setAddress(“Bangalore”)
 
Note : If you find any thing which you can not understand or wrong please leave comments.I assure i will get back to you.
 
NOTE : TO download the velocity tutorial in word format click here

5 comments:

  1. Hey!! can i process a .jsp file using velocity? Actually i'm trying to.. And while doing that actually my resource is kept in a folder different from my java program ie.
    I'm having an action class which is located at: /StrutsApp/src/packdao/InsertEmployeeAction.java
    and my templateFile is located at: /StrutsApp/WebContent/JSP/confirmation.jsp
    but im getting “Unable to find resource” error.
    Can u please please help me ??

    ReplyDelete
  2. hi sanketh .. this is shiva , we con't process.jsp file .. it should be a textfilem, we can place the .txt file any where, but the proper path we have to mention in getTemplate();

    ReplyDelete
  3. Can the variable name starts with _(underscore) , like $_var1 etc.
    It is able to parse and replace with values.

    ReplyDelete
  4. Can you post few more example with the latest version

    ReplyDelete