java Velocity 同時初始化兩個 Engine 實例

AraReddick 9年前發布 | 12K 次閱讀 Java HTML5 JavaScript

兩個不同的 Velocity Engine 實例,使用不同的配置

VelocityEngineTest.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.VelocityEngine;

public class VelocityEngineTest {

  private static final String TEMPLATE_NAME = "VelocityEngineTest.vm";

  public static void main(String[] args) throws Exception {
    Properties p1 = new Properties();
    p1.put("resource.loader", "file");
    p1.put("file.resource.loader.class",
        "org.apache.velocity.runtime.resource.loader.FileResourceLoader");
    p1.put("file.resource.loader.path", "./src/ve1");

    Properties p2 = new Properties();
    p2.put("resource.loader", "file");
    p2.put("file.resource.loader.class",
        "org.apache.velocity.runtime.resource.loader.FileResourceLoader");
    p2.put("file.resource.loader.path", "./src/ve2");

    //now create the engines
    VelocityEngine ve1 = new VelocityEngine();
    ve1.init(p1);

    VelocityEngine ve2 = new VelocityEngine();
    ve2.init(p2);

    // now get the output for each engine
    writeTemplateOutput(ve1);
    writeTemplateOutput(ve2);

  }

  public static void writeTemplateOutput(VelocityEngine ve) throws Exception {
    Template t = ve.getTemplate(TEMPLATE_NAME);

    Writer writer = new StringWriter();
    t.merge(new VelocityContext(), writer);

    System.out.println(writer.toString());
  }
}
 本文由用戶 AraReddick 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!