Knowledge Walls
John Peter
Pune, Maharashtra, India
How to use #continue in foreach of Java apache velocity with Example
14733 Views
#continue 
#continue is not in apache velocity.

This is wrong. because #continue is not in apache velocity
#foreach ($index in [1..5])
    #if ($i == 3)
       #continue
    #end

    I value is $i
#end

So use
#foreach ($index in [1..5])
    #if ($i != 3)
        I value is $i
    #end
#end
ExampleWithoutContinue
import java.io.StringWriter;

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

public class ExampleWithoutContinue {
    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)
i=$i
#end
#end
Velocity is End!
Output 
i=1
i=2
i=3
i=4
i=6
i=7
i=8
i=9
i=10
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