Knowledge Walls
John Peter
Pune, Maharashtra, India
Hello world basic java velocity Example in Examples of Java Apache Velocity Examples
21457 Views
Hints 
Hello World example for basic velocity program to write username value into datapage.vm template via java.

VelocityEngine class is used to parse the Velocity template (vm) file. VelocityContext contians all the data which is needed to bind in (.vm) file. StringWriter stream writes the data into the stream.
Example
import java.io.StringWriter;

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

public class SampleVelocity {
    public static void main(String args[]) throws Exception {
        VelocityEngine ve = new VelocityEngine();
        ve.init();
        
        Template t = ve.getTemplate("datapage.vm");
        
        VelocityContext vc = new VelocityContext();
            vc.put("username", "John");
            
        StringWriter sw = new StringWriter();
        t.merge(vc, sw);
        
        System.out.println(sw);
    }
}
Datapage.vm 
Username is $username.
Output 
Username is John.
Best Lessons of "Java Apache Velocity Examples"
Top lessons which are viewed more times.
  Copyright © 2014 Knowledge walls, All rights reserved
KnowledgeWalls
keep your tutorials and learnings with KnowledgeWalls. Don't lose your learnings hereafter. Save and revise it whenever required.
Click here for more details