Knowledge Walls
John Peter
Pune, Maharashtra, India
How to use Else-If ladder in Java velocity with Example
11832 Views
ElseIfLadderJavaVelocity
import java.io.StringWriter;

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

public class ElseIfLadderJavaVelocity {
    public static void main(String args[]){
        VelocityEngine engine = new VelocityEngine();
        engine.init();
        
        Template template = engine.getTemplate("userinfo.vm");
        
        VelocityContext context = new VelocityContext();
            context.put("choice", 4);
            
        StringWriter sw = new StringWriter();
        template.merge(context, sw);
        
        System.out.println(sw.toString());
    }
}
Userinfo.vm 
User Details
------------
#if ($choice == 1)
You selected one.
#elseif ($choice == 2)
You selected two.
#elseif ($choice == 3)
You selected three.
#elseif ($choice == 4)
You selected four.
#else
You have to select 1 to 4.
#end
Output 
User Details
------------
You selected four.
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