velocity模板加載的三種形式

本文針對velocity模板加載的三種方式進行說明java

一:velocity默認加載方式(文件加載)  (下面是針對文件加載的目錄結構)git

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();
    }
}

具體實現請看:https://git.oschina.net/ldbangel/quicksurepcprovider/blob/master/src/main/java/com/quicksure/insurance/velocity/VelocityTemplate.javaapache

二:從類路徑加載模板文件app

package com.velocity.test;

import java.io.StringWriter;
import java.util.Properties;

import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;

/**
 * 從class(類路徑)中加載模板文件
 * @author welcome
 *
 */
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文件中加載模板文件ide

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();
    }
}

實例展現:https://git.oschina.net/ldbangel/quicksurepcprovider/blob/master/src/main/java/com/quicksure/insurance/velocity/VelocityTemplate.javaui

主要應該就是用的file文件加載和jar包文件加載spa

相關文章
相關標籤/搜索