Knowledge Walls
John Peter
Pune, Maharashtra, India
How to use If statement in Java velocity with Example
8759 Views
IFElseStatementVelocity
import java.io.StringWriter;

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

public class IFElseStatementVelocity {
    public static void main(String args[]){
        VelocityEngine engine = new VelocityEngine();
        engine.init();
        
        Template template = engine.getTemplate("userinfo.vm");
        
        VelocityContext context = new VelocityContext();
            context.put("maximum_users", 10);
            
        StringWriter sw = new StringWriter();
        template.merge(context, sw);
        
        System.out.println(sw.toString());
    }
}
Userinfo.vm 
User Details
------------
#if ($maximum_users <= 10)
Minimum no of users only allowed. $user_limit may enters to the room.
#else
Maximum no of users are allowed.
#end
Output 
User Details
------------
Minimum no of users only allowed. 10 may enters to the room.
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