Knowledge Walls
John Peter
Pune, Maharashtra, India
How to use expressions with #set method of java apache velocity with Example
5531 Views
SetMethodInVTLExample
import java.io.StringWriter;

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

public class Tester {
    public static void main(String args[]){
        VelocityEngine ve = new VelocityEngine();
        ve.init();
         
        Template t = ve.getTemplate("velocitytemplate.vm");
         
        VelocityContext vc = new VelocityContext();
            vc.put("num1", 20);
            vc.put("num2", 40);
             
        StringWriter sw = new StringWriter();
        t.merge(vc, sw);
         
        System.out.println(sw);
    }
}
Velocitytemplate.vm 
Adding Two Numbers
---------------------
Num1 = $num1;
Num2 = $num2;

#set($answer = $num1 + $num2)
Answer = Num1 + Num2
Answer = $answer;
Output 
Adding Two Numbers
---------------------
Num1 = 20;
Num2 = 40;

Answer = Num1 + Num2
Answer = 60;
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