Home Apachevelocity How to pass java.util.Properties in properties
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

How to pass java.util.Properties in properties

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

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
Advertisements

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: August 27, 2008

Ask Questions?    Discuss: How to pass java.util.Properties in properties  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com