lsdwyl 发表于 2017-1-4 06:22:40

apache VelocityEngine使用记录

  VelocityEngine是一个模板引擎,能够基于模板生成指定的文件代码。
  使用方法如下:
  VelocityEngine engine = new VelocityEngine();// 定义模板引擎
  Properties properties = new Properties();// 模板引擎属性
  properties.setProperty(Velocity.RESOURCE_LOADER, "file");// 定义资源加载器类型,此处为file
  properties.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, path); // 定义文件加载路径path
  engine.init(properties);// 根据定义的属性,初始化模板引擎
  
    // 定义模板替换上下文
  VelocityContext context = new VelocityContext();
    context.put("orgname", (String)param.get("orgname"));
    context.put("account", (String)param.get("account"));
    context.put("startdate", (String)param.get("startdate"));
     。。。。。。
  // 获取模板并写入到指定的文件中,同时将模板文件中的占位符替换成context中的内容。    
  Template template = engine.getTemplate((String)param.get("vmname"), "GBK"); // 获取模板
  String tmpname = UUID.randomUUID().toString()+".html";
    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(path+tmpname)), "GBK"));
    template.merge(context, writer);
   writer.flush();
   writer.close();
页: [1]
查看完整版本: apache VelocityEngine使用记录