How to pass java.util.Properties in properties

This Example shows you how to use java.util.Properties to in velocity.

How to pass java.util.Properties in properties

How to pass java.util.Properties in properties

     

This Example shows you how to use java.util.Properties to in velocity. The methods used in this example are described below:- 
1:- Initialize velocity run time engine through method  init().
2:- Create object of VelocityContext Class.
3:- Create Template class object, Template  class object is used for controlling template methods and properties.

In this example we have used method init() with parameter of type Properties type that initialize velocity runtime engine with default properties plus properties passed as parameter in init() method. 

template.merge(context, writer): Merge method of the Template class is used here for merging  the VelocityContext class object to produce the output.


 

PassProperties.java

import java.io.StringWriter;
import java.io.Writer;
import java.util.Properties;

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

public class PassProperties {

  public static void main(String[] argsthrows Exception {
  Properties props = new Properties();
  props.put("input.encoding""utf-8");

  Velocity.init(props);

  Template template = Velocity.getTemplate("./src/PassProperties.vm");

  VelocityContext context = new VelocityContext();

  Writer writer = new StringWriter();
  template.merge(context, writer);

  System.out.println(writer.toString());
  }
}

PassProperties.vm
Hi,this is Komal Choudhary

Output:
Hi,this is Komal Choudhary

Download code