Java/Velocity/Velocity Properties

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

Use java.util.Properties to pass in properties

   <source lang="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 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!

      </source>
   
  
 
  



Velocity with External Properties

   <source lang="java">

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());
 }

}


  1. This is a simple example of a velocity properties file.
  2. Any property that is not listed here will have it"s default
  3. value used. The default values are located in :
  4. * src/java/org/apache/velocity/runtime/default/velocity.defaults
  5. * http://jakarta.apache.org/velocity/developer-guide.html
  6. as an example, we are changing the name of the velocity log

runtime.log = velocity_example.log


Hello World!

      </source>