velocity模板加载的三种形式

文针对velocity模板加载的三种方式进行说明

一:velocity默认加载方式(文件加载)  (下面是针对文件加载的目录结构)

 

1.直接读取资源文件方式

public class VelocityTemplate {
    public static String getVelocityTemplate(String basePath) throws Exception {
        String sysRoot = VelocityTemplate.class.getResource("").getPath();
        Properties properties = new Properties();
        //设置velocity资源加载方式为file
        properties.setProperty("resource.loader", "file");
        //设置velocity资源加载方式为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");
        //实例化一个VelocityEngine对象
        VelocityEngine velocityEngine = new VelocityEngine(properties);
        //实例化一个VelocityContext
        VelocityContext velocityContext = new VelocityContext();
        //向VelocityContext中放入键值
        context.put("username", "张三");
        context.put("password", "123456789");
        context.put("age", "20");
        //实例化一个StringWriter
        StringWriter writer=new StringWriter();
        Template template = velocityEngine.getTemplate(sysRoot+basePath, "UTF-8");
        template.merge(velocityContext, stringWriter);
        return stringWriter.toString();
    }
}

 2  从类路径加载模板文件

package com.velocity.test;
import java.io.StringWriter;
import java.util.Properties;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
/**
 * 从class(类路径)中加载模板文件
 */
public class LoaderFromClass {
    public static void main(String[] args) throws Exception{
        //初始化参数
        Properties properties=new Properties();
        //设置velocity资源加载方式为class
        properties.setProperty("resource.loader", "class");
        //设置velocity资源加载方式为file时的处理类
        properties.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        //实例化一个VelocityEngine对象
        VelocityEngine velocityEngine=new VelocityEngine(properties);
        
        //实例化一个VelocityContext
        VelocityContext context=new VelocityContext();
        //向VelocityContext中放入键值
        context.put("username", "张三");
        context.put("password", "123456789");
        context.put("age", "20");
        context.put("address", "陕西西安"); 
        context.put("blog", "http://blogjava.net/sxyx2008");
        //实例化一个StringWriter
        StringWriter writer=new StringWriter();
        //从src目录下加载hello.vm模板
        //假若在com.velocity.test包下有一个hello.vm文件,那么加载路径为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());
    }
}

 

从jar文件中加载模板文件

public class VelocityTemplate {
    public static String getVelocityTemplate(String basePath) throws Exception {
        String sysRoot = VelocityTemplate.class.getResource("").getPath();
        //初始化参数
        Properties properties=new Properties();
        //设置velocity资源加载方式为jar
        properties.setProperty("resource.loader", "jar");
        //设置velocity资源加载方式为file时的处理类
        properties.setProperty("jar.resource.loader.class", "org.apache.velocity.runtime.resource.loader.JarResourceLoader");
        //设置jar包所在的位置
        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");
        //实例化一个VelocityEngine对象
        VelocityEngine velocityEngine = new VelocityEngine(properties);
        //实例化一个VelocityContext
        VelocityContext velocityContext = new VelocityContext();
        //向VelocityContext中放入键值
        context.put("username", "张三");
        context.put("password", "123456789");
        context.put("age", "20");
        //实例化一个StringWriter
        StringWriter writer=new StringWriter();
        Template template = velocityEngine.getTemplate("com/quicksure/insurance/velocity/template/"+basePath, "UTF-8");
        template.merge(velocityContext, stringWriter);
        return stringWriter.toString();
    }
}

 

 

猜你喜欢

转载自wm920.iteye.com/blog/2410189