First, the class path
Load the vm file in the classpath directory, or the resources path of the maven project
Properties p = new Properties(); p.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); Velocity.init(p); ... Velocity.getTemplate(templateFile);
Second, the diameter of the road
Loaded by absolute path, the template file is located on disk, such as: D:\\template\\index.vm
Properties p = new Properties(); p.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH, "D:\\template"); Velocity.init(p); ... Velocity.getTemplate("index.vm");
Three, properties
Load the template file under the classpath by loading the configuration in the properties file
Properties p = new Properties(); p.load(this.getClass().getResourceAsStream("/velocity.properties")); Velocity.init(p); ... Velocity.getTemplate(templateFile);