Loading
Velocity Template Engine

Java Quick Notes

Refresh Your Java - Before Java Interview

We are Agile, believe in less Documentation - Only Quick notes (Java Interview Questions) of Java/J2ee Read more....


Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet

Go to SiteMap

Q)  Velocity Template Engine Example ?


Ans)

Step 1: Create a .vm file

sayHello.vm:
  Hello $name !  How are you ?

Sample : input this to ve.getTemplate()
 
import java.io.StringWriter;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
public class HelloWorld
{
    public static void main( String[] args )
        throws Exception
    {
        /* initialize an engine  */
        VelocityEngine ve = new VelocityEngine();
        ve.init();
  
        /*  get the Template  */
        Template t = ve.getTemplate( "sayHello.vm" );
       
  /*  create a contex */
        VelocityContext context = new VelocityContext();
        context.put("name", "John");
  
        StringWriter writer = new StringWriter();
       
  t.merge( context, writer );
  
        System.out.println( writer.toString() );    
    }
}


Output : Please see below Name is replaced with "John"

Hello John !  How are you ?



Back to top

------------------------- We hope you got necessary Info On -----------------------------------------

Velocity Template Engine


Velocity Template Engine

-------------------------------------------------------------------------------------------------------



Face Book
Request for a Mock Interview/Training

Get a PDF

Face Book
Same look (Read) on any device, this is Ads free