Wednesday, 12 December 2012

Velocity Template Tutorial _1

VELOCITY  TUTORIAL _1

TEMPLATE ENGINE :
    It is program or software which takes text files as input and process then and produces one or more output files [O/p files has content different from input text file or very few times same as input text file]
  
VELOCITY :
  • Velocity is open-source template engine written in java-language .
USES OF VELOCITY:
  • It is used for presenting a view to the user as like JSP.
  • It is used to create any kind of pages like HTML , XML and so on.
  • It can be used in standlone application to create XML files dynamically.
  • We can make the whole-text file as string which is used for generating e-mail.
  • It is easy to understand compared to JSP pages which uses Scriptlets, Tags .. so non-java developers can easily understand Velocity so Presentation layer and business layer is separated.
VELOCITY VS JSP :
  • Velocity is faster compared to JSP [JSP involves conversion to servlet then compile and run].
  • Easy to learn.
  • No need for any web-container or server to run the velocity
  • No expection occurs in velocity while in JSP we have Compile and runtime error.
      
    HELLO WORLD PROGRAM :
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

Steps involved in creating and running a velocity program.
  • STEP 1: Intialize Velocity-Engine
    [NOTE: In version 1.1 we directly intialize velocity engine by calling “Velocity.init()” method but in 1.2 we can create VelocityEngine object and then intialize the velocity Engine as “VelocityEngine velocityEngine = new VelocityEngine(); velocityEngine.init();”]
  • STEP 2: Create the Template object by calling getTemplate() method.
    [NOTE : In version 1.1 we use Velocity class to create template object as “Template template = Velocity.getTemplate("./src/directives.vm"); but in 1.2 version we can also use VelocityEngine object to create a template object as “VelocityEngine velocityEngine = new VelocityEngine(); velocityEngine.init();Template template = velocityEngine.getTemplate(/*Give the file location here*/"helloworld.txt");”]
  • STEP 3: Create VelocityContext object and add values.
  • STEP 4 : Process the template.
PROGRAMMING :

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 {

/*STEP 1 :Create a VelocityTemplate Engine Object*/
VelocityEngine velocityEngine = new VelocityEngine();
/*STEP 2 :Initialise VelocityTemplate Engine */
velocityEngine.init();
/*
* NOTE : we can directly initialise the velocity engine by calling "Velocity.init()"
* Creation of VelocityEngine object is available from 1.2 version only
* */
/*STEP 3: Create the template object by calling getTemplate() method in VE(VelocityEngine)*/
Template template = velocityEngine.getTemplate(/*Give the file location here*/"./src/helloworld.txt");
/*STEP 4: Create VelocityContext */
VelocityContext context = new VelocityContext();
StringWriter writer = new StringWriter();
/*STEP 5: Process the template and write the o/p to stream*/
template.merge(context, writer);
/*STEP 6: Show the output */
System.out.println(writer);
}
}

helloworld.txt-FILE

Hello java! Welcome to Velocity!

Hello World

NOTE : This text file is called as velocity template file
OUTPUT
Hello java! Welcome to Velocity!

Hello World
   


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

13 comments:

  1. What do you do so that instead of plain old console output, you'd be able to create a web-app screen? I've seen many tutorials but most of the classes they use are deprecated.

    ReplyDelete
  2. Hi i am a total begineer to apache velocity....can you tel me the essentials..like creating a velocityservlet,vm template

    ReplyDelete
    Replies
    1. me too....i also want to learn from basics

      Delete
  3. Thank you so much for the tutorial vignesh

    ReplyDelete
  4. plz tell me how to run velociry application in eclipse.. and how to test the already available application

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. //To read the file
    ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
    ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
    Use the above code to read the file, else it will show unable to load the resource error. Add it after velocity engine.

    ReplyDelete
  7. plz tell me how to display image in velocity , i have created one template to send mail which contain an image which is fixed, but when i receive mail it is not displaying the image .
    plz reply as soon as possible

    ReplyDelete
  8. plz tell me how to display image in velocity , i have created one template to send mail which contain an image which is fixed, but when i receive mail it is not displaying the image .
    plz reply as soon as possible bgvinay6@gmail.com

    ReplyDelete
  9. plz tell me how to display image in velocity , i have created one template to send mail which contain an image which is fixed, but when i receive mail it is not displaying the image .
    plz reply as soon as possible bgvinay6@gmail.com

    ReplyDelete
  10. plz tell me how to display image in velocity , i have created one template to send mail which contain an image which is fixed, but when i receive mail it is not displaying the image .
    plz reply as soon as possible

    ReplyDelete
  11. plz we want from basics bro....Anyway Thanks

    ReplyDelete