Knowledge Walls
John Peter
Pune, Maharashtra, India
How to use For loop in Java velocity with Example
14347 Views
ForLoopInJavaVelocity
import java.io.StringWriter;

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

public class ForLoopInJavaVelocity {
    public static void main(String args[]){
        VelocityEngine engine = new VelocityEngine();
        engine.init();
        
        Template template = engine.getTemplate("velocitytemplate.vm");
        
        VelocityContext context = new VelocityContext();
            context.put("max_n", 10);
            
        StringWriter sw = new StringWriter();
        template.merge(context, sw);
        
        System.out.println(sw.toString());
    }
}
Velocitytemplate.vm 
#foreach ($i in [1..$max_n])
i = $i;
#end
Output 
i = 1;
i = 2;
i = 3;
i = 4;
i = 5;
i = 6;
i = 7;
i = 8;
i = 9;
i = 10;
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