• velocity模板加载的三种形式


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

    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.java

    二:从类路径加载模板文件

    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文件中加载模板文件

    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();
        }
    }
  • 相关阅读:
    UVAlive5135_Mining Your Own Business
    UVAlive3523_Knights of the Round Table
    UVA10759_Dice Throwing
    POJ3177_Redundant Paths
    UVA11027_Palindromic Permutation
    Codechef_JULY14
    UVAlive4255_Guess
    UVA10054_The Necklace
    杜教BM
    【2018徐州网络赛】Morgana Net (矩阵快速幂)
  • 原文地址:https://www.cnblogs.com/interdrp/p/15704335.html
Copyright © 2020-2023  润新知