初学velocity

跟学习其它技术一样,首先到官网去下载必要的包,下载地址:http://velocity.apache.org/download.cgi目前使用的是velocity 1.6.3,由于自己的E文水平一般,在使用之前也到网上搜索了相关文章,然后根据前辈们的指导和自己的实践结合.记录下此文,以便以后能快速回忆.

 

一、在eclipse 中新建一个工程,把包velocity-1.6.3.jar到在WEB-INF/lib下,

 

二、新建了一个hello.vm的测式模板

 

Html代码   收藏代码
  1. <html>  
  2.     <head></head>  
  3.     <body>  
  4.         HELLO! $name,Welcome to velocity!  
  5.     </body>  
  6. </html>  
  7.       

 

三、新建一个java属性文件 velocity.properties,参考了别人的配置示例,详细的说明以后再理解

 

Java代码   收藏代码
  1. #Velocity.properties配置示例   
  2. # 如果需要系统从WEB-INF/classes路径加载Velocity的模板文件,取消下面两行的注释   
  3. #resource.loader=class   
  4. #class.resource.loader.class=org.apache.Velocity.runtime.resource.loader.ClasspathResourceLoader   
  5. #如需禁止系统通过文件系统加载模板文件,注释如下两行   
  6. resource.loader=file   
  7. file.resource.loader.path=D:\Workspaces\MyEclipse 8.5\velocity\WebRoot\WEB-INF\velocityTempalte  
  8. #确定从何处加载velocity的模板文件   
  9. file.resource.loader.cache=false   
  10. #设置读取模板文件的解码格式,GB2312是为了支持中文   
  11. input.encoding=gb2312   
  12. #配置输出视图文件的解码格式,GB2312是为了支持中文   
  13. output.encoding=gb2312   

 

 四、新建一个测式类VelocityTest.java

Java代码   收藏代码
  1. package velocity.test;  
  2.   
  3. import java.io.FileOutputStream;  
  4. import java.io.PrintWriter;  
  5. import java.io.StringWriter;  
  6.   
  7. import org.apache.velocity.Template;  
  8. import org.apache.velocity.VelocityContext;  
  9. import org.apache.velocity.app.Velocity;  
  10.   
  11. public class VelocityTest {  
  12.   
  13.     public static void main(String[] args) {  
  14.   
  15.         try {  
  16.             // 初始化  
  17.             Velocity.init("D:\\Workspaces\\MyEclipse 8.5\\velocity\\WebRoot\\WEB-INF\\velocity.properties");  
  18.               
  19.             //取得velocity上下文  
  20.             VelocityContext context = new VelocityContext();  
  21.             context.put("name""sea");  
  22.               
  23.             Template template = Velocity.getTemplate("hello.vm");  
  24.             StringWriter writer = new StringWriter();             
  25.             template.merge(context, writer);  
  26.               
  27.             PrintWriter filewriter = new PrintWriter(new FileOutputStream("d:\\a.html"),true);  
  28.             filewriter.println(writer.toString());  
  29.             filewriter.close();  
  30.   
  31.         } catch (Exception e) {  
  32.             e.printStackTrace();  
  33.         }  
  34.   
  35.     }  
  36.   
  37. }  

 

最后运行该类时出现如下错误:

Java代码   收藏代码
  1. Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/collections/ExtendedProperties  

 

发现缺少了集合包,把velicity.1.6.3解压后的lib目录下的commons-collections-3.2.1.jar拷入工程的lib目录下,继续运行,还是出现以后错误:

Java代码   收藏代码
  1. org.apache.velocity.exception.VelocityException: Failed to initialize an instance of org.apache.velocity.runtime.log.ServletLogChute with the current runtime configuration.  
  2.     at org.apache.velocity.runtime.log.LogManager.createLogChute(LogManager.java:206)  
  3.     at org.apache.velocity.runtime.log.LogManager.updateLog(LogManager.java:255)  
  4.     at org.apache.velocity.runtime.RuntimeInstance.initializeLog(RuntimeInstance.java:795)  
  5.     at org.apache.velocity.runtime.RuntimeInstance.init(RuntimeInstance.java:250)  
  6.     at org.apache.velocity.runtime.RuntimeInstance.init(RuntimeInstance.java:615)  
  7.     at org.apache.velocity.runtime.RuntimeSingleton.init(RuntimeSingleton.java:243)  
  8.     at org.apache.velocity.app.Velocity.init(Velocity.java:93)  
  9.     at velocity.test.VelocityTest.main(VelocityTest.java:17)  
  10. Caused by: java.lang.UnsupportedOperationException: Could not retrieve ServletContext from application attributes  
  11.     at org.apache.velocity.runtime.log.ServletLogChute.init(ServletLogChute.java:73)  
  12.     at org.apache.velocity.runtime.log.LogManager.createLogChute(LogManager.java:157)  
  13.     ... 7 more  

 最后分别把 commons-lang-2.4.jar和commons-logging-1.1.jar拷入lib目录才正常

 

输出d:\a.html下的文件如下

Java代码   收藏代码
  1. <html>  
  2.     <head></head>  
  3.     <body>  
  4.         HELLO! sea,Welcome to velocity!  
  5.     </body>  
  6. </html>  
  7.       

你可能感兴趣的:(初学velocity)