1. 程式人生 > >velocity模板加載

velocity模板加載

etc syn bar try Language iter runtime () pattern

http://hi.baidu.com/ly_dayu/item/828b09c5c3c5e547a8ba9409
velocity使用基本來說比較簡單,但在加載模板時老出問題,很多初學者經常會遇到找不到模板這種異常。本文就針對目前常用的三種模板加載方式做以說明。
技術分享

一、velocity默認的加載方式(文件加載方式)

Java代碼 技術分享
  1. package com.velocity.test;
  2. import java.io.StringWriter;
  3. import java.util.Properties;
  4. import org.apache.velocity.VelocityContext;
  5. import org.apache.velocity.app.VelocityEngine;
  6. /**
  7. * 從文件中加載模板文件,即velocity默認的模板文件加載方式
  8. * @author welcome
  9. *
  10. */
  11. public class LoaderFromFile {
  12. public static void main(String[] args) throws Exception{
  13. //初始化參數
  14. Properties properties=new Properties();
  15. //設置velocity資源加載方式為file
  16. properties.setProperty("resource.loader", "file");
  17. //設置velocity資源加載方式為file時的處理類
  18. properties.setProperty("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.FileResourceLoader");
  19. //實例化一個VelocityEngine對象
  20. VelocityEngine velocityEngine=new VelocityEngine(properties);
  21. //實例化一個VelocityContext
  22. VelocityContext context=new VelocityContext();
  23. //向VelocityContext中放入鍵值
  24. context.put("username", "張三");
  25. context.put("password", "123456789");
  26. context.put("age", "20");
  27. context.put("address", "陜西西安");
  28. context.put("blog", "http://blogjava.net/sxyx2008");
  29. //實例化一個StringWriter
  30. StringWriter writer=new StringWriter();
  31. //從vm目錄下加載hello.vm模板,在eclipse工程中該vm目錄與src目錄平級
  32. velocityEngine.mergeTemplate("vm/hello.vm", "gbk", context, writer);
  33. System.out.println(writer.toString());
  34. }
  35. }




二、從類路徑加載模板文件

Java代碼 技術分享
  1. package com.velocity.test;
  2. import java.io.StringWriter;
  3. import java.util.Properties;
  4. import org.apache.velocity.VelocityContext;
  5. import org.apache.velocity.app.VelocityEngine;
  6. /**
  7. * 從class(類路徑)中加載模板文件
  8. * @author welcome
  9. *
  10. */
  11. public class LoaderFromClass {
  12. public static void main(String[] args) throws Exception{
  13. //初始化參數
  14. Properties properties=new Properties();
  15. //設置velocity資源加載方式為class
  16. properties.setProperty("resource.loader", "class");
  17. //設置velocity資源加載方式為file時的處理類
  18. properties.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
  19. //實例化一個VelocityEngine對象
  20. VelocityEngine velocityEngine=new VelocityEngine(properties);
  21. //實例化一個VelocityContext
  22. VelocityContext context=new VelocityContext();
  23. //向VelocityContext中放入鍵值
  24. context.put("username", "張三");
  25. context.put("password", "123456789");
  26. context.put("age", "20");
  27. context.put("address", "陜西西安");
  28. context.put("blog", "http://blogjava.net/sxyx2008");
  29. //實例化一個StringWriter
  30. StringWriter writer=new StringWriter();
  31. //從src目錄下加載hello.vm模板
  32. //假若在com.velocity.test包下有一個hello.vm文件,那麽加載路徑為com/velocity/test/hello.vm
  33. velocityEngine.mergeTemplate("com/velocity/test/hello.vm", "gbk", context, writer);
  34. //velocityEngine.mergeTemplate("hello.vm", "gbk", context, writer);
  35. System.out.println(writer.toString());
  36. }
  37. }




三、從jar文件中加載模板文件

Java代碼 技術分享
  1. package com.velocity.test;
  2. import java.io.StringWriter;
  3. import java.util.Properties;
  4. import org.apache.velocity.VelocityContext;
  5. import org.apache.velocity.app.VelocityEngine;
  6. /**
  7. * 從jar文件中加載模板文件
  8. * @author welcome
  9. *
  10. */
  11. public class LoaderFromJar {
  12. public static void main(String[] args) throws Exception{
  13. //初始化參數
  14. Properties properties=new Properties();
  15. //設置velocity資源加載方式為jar
  16. properties.setProperty("resource.loader", "jar");
  17. //設置velocity資源加載方式為file時的處理類
  18. properties.setProperty("jar.resource.loader.class", "org.apache.velocity.runtime.resource.loader.JarResourceLoader");
  19. //設置jar包所在的位置
  20. properties.setProperty("jar.resource.loader.path", "jar:file:WebRoot/WEB-INF/lib/vm.jar");
  21. //實例化一個VelocityEngine對象
  22. VelocityEngine velocityEngine=new VelocityEngine(properties);
  23. //實例化一個VelocityContext
  24. VelocityContext context=new VelocityContext();
  25. //向VelocityContext中放入鍵值
  26. context.put("username", "張三");
  27. context.put("password", "123456789");
  28. context.put("age", "20");
  29. context.put("address", "陜西西安");
  30. context.put("blog", "http://blogjava.net/sxyx2008");
  31. //實例化一個StringWriter
  32. StringWriter writer=new StringWriter();
  33. //從/WebRoot/WEB-INF/lib/vm.jar中加載hello.vm模板 vm.jar的目錄結構為vm/hello.vm
  34. velocityEngine.mergeTemplate("vm/hello.vm", "gbk", context, writer);
  35. System.out.println(writer.toString());
  36. }
  37. }




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. import java.io.BufferedWriter;
  20. import java.io.OutputStreamWriter;
  21. import java.io.StringWriter;
  22. import java.util.ArrayList;
  23. import java.util.Properties;
  24. import org.apache.velocity.Template;
  25. import org.apache.velocity.VelocityContext;
  26. import org.apache.velocity.app.Velocity;
  27. import org.apache.velocity.app.VelocityEngine;
  28. import org.apache.velocity.exception.MethodInvocationException;
  29. import org.apache.velocity.exception.ParseErrorException;
  30. /**
  31. * This class is a simple demonstration of how the Velocity Template Engine
  32. * can be used in a standalone application using the Velocity utility class.
  33. *
  34. * It demonstrates two of the ‘helper‘ methods found in the org.apache.velocity.util.Velocity
  35. * class, mergeTemplate() and evaluate().
  36. *
  37. *
  38. * @author <a href="mailto:[email protected]">Geir Magnusson Jr.</a>
  39. * @version $Id: Example2.java 463298 2006-10-12 16:10:32Z henning $
  40. */
  41. public class Example2
  42. {
  43. public static ArrayList getNames()
  44. {
  45. ArrayList list = new ArrayList();
  46. list.add("ArrayList element 1");
  47. list.add("ArrayList element 2");
  48. list.add("ArrayList element 3");
  49. list.add("ArrayList element 4");
  50. return list;
  51. }
  52. public static void main( String args[] )
  53. {
  54. /* first, we init the runtime engine. Defaults are fine. */
  55. Properties p = new Properties();
  56. //設置輸入輸出編碼類型。和這次說的解決的問題無關
  57. p.setProperty(Velocity.INPUT_ENCODING, "UTF-8");
  58. p.setProperty(Velocity.OUTPUT_ENCODING, "UTF-8");
  59. //這裏加載類路徑裏的模板而不是文件系統路徑裏的模板
  60. p.setProperty("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
  61. //也可以用下面方法指定一個絕對路徑,不過這樣要求你所有的模板都放在該路徑下,是有局限的
  62. //p.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, "模板路徑");
  63. try
  64. {
  65. Velocity.init(p);
  66. }
  67. catch(Exception e)
  68. {
  69. System.out.println("Problem initializing Velocity : " + e );
  70. return;
  71. }
  72. /* lets make a Context and put data into it */
  73. VelocityContext context = new VelocityContext();
  74. context.put("name", "Velocity");
  75. context.put("project", "阿帕奇");
  76. context.put("list", getNames());
  77. /* lets render a template */
  78. StringWriter w = new StringWriter();
  79. try
  80. {
  81. Velocity.mergeTemplate("example2.vm", "UTF-8", context, w );
  82. }
  83. catch (Exception e )
  84. {
  85. System.out.println("Problem merging template : " + e );
  86. }
  87. System.out.println(" template : " + w );
  88. /*
  89. * lets dynamically ‘create‘ our template
  90. * and use the evaluate() method to render it
  91. */
  92. //這個例子也同時告訴我們可以先從文件系統讀取一個文件到字符串,然後進行我們想要的操作
  93. String s = "We are using $project $name to render this.";
  94. w = new StringWriter();
  95. try
  96. {
  97. Velocity.evaluate( context, w, "mystring", s );
  98. }
  99. catch( ParseErrorException pee )
  100. {
  101. /*
  102. * thrown if something is wrong with the
  103. * syntax of our template string
  104. */
  105. System.out.println("ParseErrorException : " + pee );
  106. }
  107. catch( MethodInvocationException mee )
  108. {
  109. /*
  110. * thrown if a method of a reference
  111. * called by the template
  112. * throws an exception. That won‘t happen here
  113. * as we aren‘t calling any methods in this
  114. * example, but we have to catch them anyway
  115. */
  116. System.out.println("MethodInvocationException : " + mee );
  117. }
  118. catch( Exception e )
  119. {
  120. System.out.println("Exception : " + e );
  121. }
  122. System.out.println(" string : " + w );
  123. ///////////////////////////////////////////////////////
  124. //其他方法: 1分別指定路徑,此方法可以設定不同的路徑 (也可是相對的。在eclipse下是工程目錄)
  125. try {
  126. VelocityEngine velocityEngine = new VelocityEngine();
  127. Properties properties = new Properties();
  128. //也可以在這裏指定絕對路徑。當指定相對路徑時, 在不同的環境下是有區別的。
  129. //比如把程序部署到tomcat以後,相對路徑相對到哪裏是個很惡心的事情。
  130. String basePath = "vm";
  131. //可設置絕對路徑
  132. //String basePath = "F:/";
  133. properties.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, basePath);
  134. velocityEngine.init(properties);
  135. Template template = velocityEngine.getTemplate("example2.vm");
  136. BufferedWriter writer = new BufferedWriter(
  137. new OutputStreamWriter(System.out));
  138. template.merge(context, writer);
  139. writer.flush();
  140. writer.close();
  141. } catch (Exception e) {
  142. e.printStackTrace();
  143. }
  144. }
  145. }

velocity模板加載