一、类路径

  加载classpath目录下的vm文件,或者maven项目的resources路径下

Properties p = new Properties();
p.put("file.resource.loader.class",
"org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
Velocity.init(p);
...
Velocity.getTemplate(templateFile);

二、盘路径

  通过绝对路径来加载,模版文件位于磁盘中,如:D:\\template\\index.vm

Properties p = new Properties();
p.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH, "D:\\template");
Velocity.init(p);
...
Velocity.getTemplate("index.vm");

三、properties

  通过加载properties文件中的配置,加载类路径下的模版文件 

Properties p = new Properties();
p.load(this.getClass().getResourceAsStream("/velocity.properties"));
Velocity.init(p);
...
Velocity.getTemplate(templateFile);

  

 

相关文章:

  • 2022-12-23
  • 2021-11-01
  • 2021-08-01
  • 2021-11-27
  • 2021-10-23
  • 2022-12-23
猜你喜欢
  • 2021-08-18
  • 2022-02-24
  • 2021-09-11
  • 2022-12-23
相关资源
相似解决方案