Java/Velocity/Velocity Properties

Материал из Java эксперт
Перейти к: навигация, поиск

Use java.util.Properties to pass in properties

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 HelloWorldProperties {
  public static void main(String[] args) throws Exception {
    Properties props = new Properties();
    props.put("input.encoding", "utf-8");
    Velocity.init(props);
    Template template = Velocity.getTemplate("./src/HelloWorld.vm");
    VelocityContext context = new VelocityContext();
    Writer writer = new StringWriter();
    template.merge(context, writer);
    System.out.println(writer.toString());
  }
}
-------------------------------------------------------------------------------------
//File: HelloWorld.vm
Hello World!





Velocity with External Properties

import java.io.StringWriter;
import java.io.Writer;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
public class HelloWorldExternalProperties {
  public static void main(String[] args) throws Exception {
    Velocity.init("./src/velocity.properties");
    Template template = Velocity.getTemplate("./src/HelloWorld.vm");
    VelocityContext context = new VelocityContext();
    Writer writer = new StringWriter();
    template.merge(context, writer);
    System.out.println(writer.toString());
  }
}
-------------------------------------------------------------------------------------
#
# This is a simple example of a velocity properties file.
#
# Any property that is not listed here will have it"s default
# value used.  The default values are located in :
#  *  src/java/org/apache/velocity/runtime/default/velocity.defaults
#  *  http://jakarta.apache.org/velocity/developer-guide.html
#
# as an example, we are changing the name of the velocity log 
#
runtime.log = velocity_example.log

-------------------------------------------------------------------------------------
Hello World!