This article explains the three ways of loading velocity templates
1: Velocity default loading method (file loading) (the following is the directory structure for file loading)
1. Directly read the resource file method
public class VelocityTemplate { public static String getVelocityTemplate(String basePath) throws Exception { String sysRoot = VelocityTemplate.class.getResource("").getPath(); Properties properties = new Properties(); / / Set the velocity resource loading method to file properties.setProperty("resource.loader", "file"); //Set the processing class when the velocity resource loading method is file properties.setProperty("file.resource.loader.class","org.apache.velocity.runtime.resource.loader.FileResourceLoader"); properties.put("input.encoding", "UTF-8"); properties.put("output.encoding", "UTF-8"); //Instantiate a VelocityEngine object VelocityEngine velocityEngine = new VelocityEngine(properties); //Instantiate a VelocityContext VelocityContext velocityContext = new VelocityContext(); //Put key values into VelocityContext context.put("username", "张三"); context.put("password", "123456789"); context.put("age", "20"); //Instantiate a StringWriter StringWriter writer=new StringWriter(); Template template = velocityEngine.getTemplate(sysRoot+basePath, "UTF-8"); template.merge(velocityContext, stringWriter); return stringWriter.toString(); } }
2 Load the template file from the classpath
package com.velocity.test; import java.io.StringWriter; import java.util.Properties; import org.apache.velocity.VelocityContext; import org.apache.velocity.app.VelocityEngine; /** * Load template files from class (classpath) */ public class LoaderFromClass { public static void main(String[] args) throws Exception{ //Initialization parameters Properties properties=new Properties(); / / Set the velocity resource loading method to class properties.setProperty("resource.loader", "class"); //Set the processing class when the velocity resource loading method is file properties.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); //Instantiate a VelocityEngine object VelocityEngine velocityEngine=new VelocityEngine(properties); //Instantiate a VelocityContext VelocityContext context=new VelocityContext(); //Put key values into VelocityContext context.put("username", "张三"); context.put("password", "123456789"); context.put("age", "20"); context.put("address", "Shaanxi Xi'an"); context.put("blog", "http://blogjava.net/sxyx2008"); //Instantiate a StringWriter StringWriter writer=new StringWriter(); //Load the hello.vm template from the src directory //If there is a hello.vm file under the com.velocity.test package, the loading path is com/velocity/test/hello.vm velocityEngine.mergeTemplate("com/velocity/test/hello.vm", "gbk", context, writer); //velocityEngine.mergeTemplate("hello.vm", "gbk", context, writer); System.out.println(writer.toString()); } }
3 Load the template file from the jar file
public class VelocityTemplate { public static String getVelocityTemplate(String basePath) throws Exception { String sysRoot = VelocityTemplate.class.getResource("").getPath(); //Initialization parameters Properties properties=new Properties(); //Set the velocity resource loading method to jar properties.setProperty("resource.loader", "jar"); //Set the processing class when the velocity resource loading method is file properties.setProperty("jar.resource.loader.class", "org.apache.velocity.runtime.resource.loader.JarResourceLoader"); //Set the location of the jar package properties.setProperty("jar.resource.loader.path", "jar:file:/F:/quicksure_Server_Provider.jar"); properties.put("input.encoding", "UTF-8"); properties.put("output.encoding", "UTF-8"); //Instantiate a VelocityEngine object VelocityEngine velocityEngine = new VelocityEngine(properties); //Instantiate a VelocityContext VelocityContext velocityContext = new VelocityContext(); //Put key values into VelocityContext context.put("username", "张三"); context.put("password", "123456789"); context.put("age", "20"); //Instantiate a StringWriter StringWriter writer=new StringWriter(); Template template = velocityEngine.getTemplate("com/quicksure/insurance/velocity/template/"+basePath, "UTF-8"); template.merge(velocityContext, stringWriter); return stringWriter.toString(); } }