Velocity 模板文件的加载器示例 (Resource Loader),velocityloader,ResourceLoad
分享于 点击 48521 次 点评:212
Velocity 模板文件的加载器示例 (Resource Loader),velocityloader,ResourceLoad
ResourceLoaders.java
import java.io.StringWriter;import java.io.Writer;import org.apache.velocity.Template;import org.apache.velocity.VelocityContext;import org.apache.velocity.app.Velocity;public class ResourceLoaders { private static final String FILE_RESOURCE_LOADER_TEMPLATE = "./src/ResourceLoader1.vm"; private static final String JAR_RESOURCE_LOADER_TEMPLATE = "./src/ResourceLoader2.vm"; private static final String CLASSPATH_RESOURCE_LOADER_TEMPLATE = "./src/ResourceLoader3.vm"; public static void main(String[] args) throws Exception { processTemplate(FILE_RESOURCE_LOADER_TEMPLATE); processTemplate(FILE_RESOURCE_LOADER_TEMPLATE); processTemplate(JAR_RESOURCE_LOADER_TEMPLATE); processTemplate(JAR_RESOURCE_LOADER_TEMPLATE); processTemplate(CLASSPATH_RESOURCE_LOADER_TEMPLATE); processTemplate(CLASSPATH_RESOURCE_LOADER_TEMPLATE); } private static void processTemplate(String templateName) throws Exception { Velocity.init(); Template template = Velocity.getTemplate(templateName); VelocityContext context = new VelocityContext(); Writer writer = new StringWriter(); template.merge(context, writer); System.out.println(writer.toString()); }}
三个vm文件
//File: ResourceLoader1.vmResourceLoader1 Template!//File: ResourceLoader2.vmResourceLoader2 Template Modified!//File: ResourceLoader3.vmResourceLoader3 Template 3
用户点评