Knowledge Walls
John Peter
Pune, Maharashtra, India
How to use #break in foreach of Java apache velocity with Example
13774 Views
BreakForeachJavaVelocity
import java.io.StringWriter;

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

public class BreakForeachJavaVelocity {
    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])
#if ($i == 5)
#break
#end
i=$i
#end
Velocity is End!
Output 
i=1
i=2
i=3
i=4
Velocity is End!
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