velocity模板加载

http://hi.baidu.com/ly_dayu/item/828b09c5c3c5e547a8ba9409 
velocity使用基本来说比较简单,但在加载模板时老出问题,很多初学者经常会遇到找不到模板这种异常。本文就针对目前常用的三种模板加载方式做以说明。 
 

一、velocity默认的加载方式(文件加载方式) 

Java代码  收藏代码
  1. package com.velocity.test;  
  2.   
  3. import java.io.StringWriter;  
  4. import java.util.Properties;  
  5.   
  6. import org.apache.velocity.VelocityContext;  
  7. import org.apache.velocity.app.VelocityEngine;  
  8.   
  9. /** 
  10.  * 从文件中加载模板文件,即velocity默认的模板文件加载方式 
  11.  * @author welcome 
  12.  * 
  13.  */  
  14. public class LoaderFromFile {  
  15.       
  16.     public static void main(String[] args) throws Exception{  
  17.         //初始化参数  
  18.         Properties properties=new Properties();  
  19.         //设置velocity资源加载方式为file  
  20.         properties.setProperty("resource.loader", "file");  
  21.         //设置velocity资源加载方式为file时的处理类  
  22.         properties.setProperty("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.FileResourceLoader");  
  23.         //实例化一个VelocityEngine对象  
  24.         VelocityEngine velocityEngine=new VelocityEngine(properties);  
  25.           
  26.         //实例化一个VelocityContext  
  27.         VelocityContext context=new VelocityContext();  
  28.         //向VelocityContext中放入键值  
  29.         context.put("username", "张三");  
  30.         context.put("password", "123456789");  
  31.         context.put("age", "20");  
  32.         context.put("address", "陕西西安");   
  33.         context.put("blog", "http://blogjava.net/sxyx2008");  
  34.         //实例化一个StringWriter  
  35.         StringWriter writer=new StringWriter();  
  36.         //从vm目录下加载hello.vm模板,在eclipse工程中该vm目录与src目录平级  
  37.         velocityEngine.mergeTemplate("vm/hello.vm", "gbk", context, writer);  
  38.         System.out.println(writer.toString());  
  39.           
  40.     }  
  41. }  




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

Java代码  收藏代码
  1. package com.velocity.test;  
  2.   
  3. import java.io.StringWriter;  
  4. import java.util.Properties;  
  5.   
  6. import org.apache.velocity.VelocityContext;  
  7. import org.apache.velocity.app.VelocityEngine;  
  8.   
  9. /** 
  10.  * 从class(类路径)中加载模板文件 
  11.  * @author welcome 
  12.  * 
  13.  */  
  14. public class LoaderFromClass {  
  15.       
  16.     public static void main(String[] args) throws Exception{  
  17.         //初始化参数  
  18.         Properties properties=new Properties();  
  19.         //设置velocity资源加载方式为class  
  20.         properties.setProperty("resource.loader", "class");  
  21.         //设置velocity资源加载方式为file时的处理类  
  22.         properties.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");  
  23.         //实例化一个VelocityEngine对象  
  24.         VelocityEngine velocityEngine=new VelocityEngine(properties);  
  25.           
  26.         //实例化一个VelocityContext  
  27.         VelocityContext context=new VelocityContext();  
  28.         //向VelocityContext中放入键值  
  29.         context.put("username", "张三");  
  30.         context.put("password", "123456789");  
  31.         context.put("age", "20");  
  32.         context.put("address", "陕西西安");   
  33.         context.put("blog", "http://blogjava.net/sxyx2008");  
  34.         //实例化一个StringWriter  
  35.         StringWriter writer=new StringWriter();  
  36.           
  37.         //从src目录下加载hello.vm模板  
  38.         //假若在com.velocity.test包下有一个hello.vm文件,那么加载路径为com/velocity/test/hello.vm  
  39.         velocityEngine.mergeTemplate("com/velocity/test/hello.vm", "gbk", context, writer);  
  40.           
  41.         //velocityEngine.mergeTemplate("hello.vm", "gbk", context, writer);  
  42.         System.out.println(writer.toString());  
  43.     }  
  44. }  




三、从jar文件中加载模板文件 

Java代码  收藏代码
  1. package com.velocity.test;  
  2.   
  3. import java.io.StringWriter;  
  4. import java.util.Properties;  
  5.   
  6. import org.apache.velocity.VelocityContext;  
  7. import org.apache.velocity.app.VelocityEngine;  
  8.   
  9. /** 
  10.  * 从jar文件中加载模板文件 
  11.  * @author welcome 
  12.  * 
  13.  */  
  14. public class LoaderFromJar {  
  15.       
  16.     public static void main(String[] args) throws Exception{  
  17.         //初始化参数  
  18.         Properties properties=new Properties();  
  19.         //设置velocity资源加载方式为jar  
  20.         properties.setProperty("resource.loader", "jar");  
  21.         //设置velocity资源加载方式为file时的处理类  
  22.         properties.setProperty("jar.resource.loader.class", "org.apache.velocity.runtime.resource.loader.JarResourceLoader");  
  23.         //设置jar包所在的位置  
  24.         properties.setProperty("jar.resource.loader.path", "jar:file:WebRoot/WEB-INF/lib/vm.jar");  
  25.         //实例化一个VelocityEngine对象  
  26.         VelocityEngine velocityEngine=new VelocityEngine(properties);  
  27.           
  28.         //实例化一个VelocityContext  
  29.         VelocityContext context=new VelocityContext();  
  30.         //向VelocityContext中放入键值  
  31.         context.put("username", "张三");  
  32.         context.put("password", "123456789");  
  33.         context.put("age", "20");  
  34.         context.put("address", "陕西西安");   
  35.         context.put("blog", "http://blogjava.net/sxyx2008");  
  36.         //实例化一个StringWriter  
  37.         StringWriter writer=new StringWriter();  
  38.         //从/WebRoot/WEB-INF/lib/vm.jar中加载hello.vm模板  vm.jar的目录结构为vm/hello.vm  
  39.         velocityEngine.mergeTemplate("vm/hello.vm", "gbk", context, writer);  
  40.         System.out.println(writer.toString());  
  41.     }  
  42. }  




velocity模板路径又一解http://www.blogjava.net/patterns/archive/2006/11/28/velocity_template_path_another_method.html 
研究hibernatesynchronizer的源码,看到他将velocity模板和编译的类一起打包在jar包中,在获得模板时使用 

Java代码  收藏代码
  1. Xobject.class.getClassLoader().getResourceAsStream("/templates/xx.vm")  

获得流,然后再将转变成字符串 

Java代码  收藏代码
  1. public static String getStringFromStream(InputStream is) throws IOException {  
  2.         if (null == is)  
  3.             return null;  
  4.         try {  
  5.             InputStreamReader reader = new InputStreamReader(is);  
  6.             char[] buffer = new char[1024];  
  7.             StringWriter writer = new StringWriter();  
  8.             int bytes_read;  
  9.             while ((bytes_read = reader.read(buffer)) != -1) {  
  10.                 writer.write(buffer, 0, bytes_read);  
  11.             }  
  12.             return (writer.toString());  
  13.         } finally {  
  14.             if (null != is)  
  15.                 is.close();  
  16.         }  
  17.     }  


最后调用velocity的方法 

Java代码  收藏代码
  1. Velocity.evaluate(Context context, java.io.Writer out, java.lang.String logTag, java.lang.String instring)   


从而生成文件。居然不知道velocity有这样的方法,挺无知的,为了路径焦头烂额,终于得解了。总结一下技巧: 
1、Xobject.class.getClassLoader().getResourceAsStream("/templates/xx.vm")相对路径获得流; 
2、Velocity.evaluate(...)方法使用; 


velocity模板路径: http://zhyt710.iteye.com/blog/235250 
遇到的velocity加载模板时的路径问题。 
于是查阅资料解决。最后综合velocity自己带的例子的example1和example2,改写了一个例子。怎样解决的在例子的注释中已经说的很明确。对于初学velocity的同志来说,这个例子可以是你参照学习的良好实例 

Java代码  收藏代码
  1. /* 
  2.  * Licensed to the Apache Software Foundation (ASF) under one 
  3.  * or more contributor license agreements.  See the NOTICE file 
  4.  * distributed with this work for additional information 
  5.  * regarding copyright ownership.  The ASF licenses this file 
  6.  * to you under the Apache License, Version 2.0 (the 
  7.  * "License"); you may not use this file except in compliance 
  8.  * with the License.  You may obtain a copy of the License at 
  9.  * 
  10.  *   http://www.apache.org/licenses/LICENSE-2.0 
  11.  * 
  12.  * Unless required by applicable law or agreed to in writing, 
  13.  * software distributed under the License is distributed on an 
  14.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 
  15.  * KIND, either express or implied.  See the License for the 
  16.  * specific language governing permissions and limitations 
  17.  * under the License.     
  18.  */  
  19.   
  20. import java.io.BufferedWriter;  
  21. import java.io.OutputStreamWriter;  
  22. import java.io.StringWriter;  
  23. import java.util.ArrayList;  
  24. import java.util.Properties;  
  25.   
  26. import org.apache.velocity.Template;  
  27. import org.apache.velocity.VelocityContext;  
  28. import org.apache.velocity.app.Velocity;  
  29. import org.apache.velocity.app.VelocityEngine;  
  30. import org.apache.velocity.exception.MethodInvocationException;  
  31. import org.apache.velocity.exception.ParseErrorException;  
  32.   
  33. /** 
  34.  * This class is a simple demonstration of how the Velocity Template Engine 
  35.  * can be used in a standalone application using the Velocity utility class. 
  36.  * 
  37.  * It demonstrates two of the 'helper' methods found in the org.apache.velocity.util.Velocity 
  38.  * class, mergeTemplate() and evaluate(). 
  39.  * 
  40.  * 
  41.  * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a> 
  42.  * @version $Id: Example2.java 463298 2006-10-12 16:10:32Z henning $ 
  43.  */  
  44.   
  45. public class Example2  
  46. {  
  47.       
  48.      public static ArrayList getNames()  
  49.         {  
  50.             ArrayList list = new ArrayList();  
  51.   
  52.             list.add("ArrayList element 1");  
  53.             list.add("ArrayList element 2");  
  54.             list.add("ArrayList element 3");  
  55.             list.add("ArrayList element 4");  
  56.   
  57.             return list;  
  58.         }  
  59.       
  60.     public static void main( String args[] )  
  61.     {  
  62.         /* first, we init the runtime engine.  Defaults are fine. */  
  63.   
  64.         Properties p = new Properties();  
  65.         //设置输入输出编码类型。和这次说的解决的问题无关  
  66.         p.setProperty(Velocity.INPUT_ENCODING, "UTF-8");  
  67.         p.setProperty(Velocity.OUTPUT_ENCODING, "UTF-8");  
  68.         //这里加载类路径里的模板而不是文件系统路径里的模板  
  69.         p.setProperty("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");  
  70.         //也可以用下面方法指定一个绝对路径,不过这样要求你所有的模板都放在该路径下,是有局限的  
  71.         //p.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, "模板路径");  
  72.         try  
  73.         {  
  74.             Velocity.init(p);  
  75.         }  
  76.         catch(Exception e)  
  77.         {  
  78.             System.out.println("Problem initializing Velocity : " + e );  
  79.             return;  
  80.         }  
  81.   
  82.         /* lets make a Context and put data into it */  
  83.   
  84.         VelocityContext context = new VelocityContext();  
  85.   
  86.         context.put("name", "Velocity");  
  87.         context.put("project", "阿帕奇");  
  88.           
  89.         context.put("list", getNames());  
  90.   
  91.         /* lets render a template */  
  92.   
  93.         StringWriter w = new StringWriter();  
  94.   
  95.         try  
  96.         {  
  97.             Velocity.mergeTemplate("example2.vm", "UTF-8", context, w );  
  98.         }  
  99.         catch (Exception e )  
  100.         {  
  101.             System.out.println("Problem merging template : " + e );  
  102.         }  
  103.   
  104.         System.out.println(" template : " + w );  
  105.   
  106.         /* 
  107.          *  lets dynamically 'create' our template 
  108.          *  and use the evaluate() method to render it 
  109.          */  
  110.   
  111.         //这个例子也同时告诉我们可以先从文件系统读取一个文件到字符串,然后进行我们想要的操作  
  112.         String s = "We are using $project $name to render this.";  
  113.         w = new StringWriter();  
  114.   
  115.         try  
  116.         {  
  117.             Velocity.evaluate( context, w, "mystring", s );  
  118.         }  
  119.         catch( ParseErrorException pee )  
  120.         {  
  121.             /* 
  122.              * thrown if something is wrong with the 
  123.              * syntax of our template string 
  124.              */  
  125.             System.out.println("ParseErrorException : " + pee );  
  126.         }  
  127.         catch( MethodInvocationException mee )  
  128.         {  
  129.             /* 
  130.              *  thrown if a method of a reference 
  131.              *  called by the template 
  132.              *  throws an exception. That won't happen here 
  133.              *  as we aren't calling any methods in this 
  134.              *  example, but we have to catch them anyway 
  135.              */  
  136.             System.out.println("MethodInvocationException : " + mee );  
  137.         }  
  138.         catch( Exception e )  
  139.         {  
  140.             System.out.println("Exception : " + e );  
  141.         }  
  142.   
  143.         System.out.println(" string : " + w );  
  144.           
  145.         ///////////////////////////////////////////////////////  
  146.         //其他方法: 1分别指定路径,此方法可以设定不同的路径 (也可是相对的。在eclipse下是工程目录)  
  147.         try {  
  148.             VelocityEngine velocityEngine = new VelocityEngine();  
  149.             Properties properties = new Properties();  
  150.             //也可以在这里指定绝对路径。当指定相对路径时, 在不同的环境下是有区别的。  
  151.             //比如把程序部署到tomcat以后,相对路径相对到哪里是个很恶心的事情。  
  152.             String basePath = "vm";  
  153.             //可设置绝对路径  
  154.             //String basePath = "F:/";  
  155.             properties.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, basePath);  
  156.             velocityEngine.init(properties);  
  157.             Template template = velocityEngine.getTemplate("example2.vm");  
  158.             BufferedWriter writer = new BufferedWriter(  
  159.                     new OutputStreamWriter(System.out));  
  160.             template.merge(context, writer);  
  161.             writer.flush();  
  162.             writer.close();  
  163.         } catch (Exception e) {  
  164.             e.printStackTrace();  
  165.         }  
  166.     }  
  167. }  
原文地址:https://www.cnblogs.com/justuntil/p/7152283.html