velocity1.7小例子

1、新建一个java工程,将velocity的jar包添加进编译路径,包括velocity-1.7.jar和lib中的所有jar包,这里给出下载链接:http://velocity.apache.org/download.cgi

2、给出模板文件:example.vm.

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <list>  
  3. #foreach( $name in $list )  
  4.     $name is fun!  
  5. #end  
  6. </list>  

3、给出我的测试类,将模板文件和类文件放在一个目录下。

  1. import java.io.BufferedWriter;  
  2. import java.io.OutputStreamWriter;  
  3. import java.util.ArrayList;  
  4. import org.apache.velocity.Template;  
  5. import org.apache.velocity.VelocityContext;  
  6. import org.apache.velocity.app.Velocity;  
  7. import org.apache.velocity.exception.ParseErrorException;  
  8. import org.apache.velocity.exception.ResourceNotFoundException;  
  9. public class HelloVeloctiy {  
  10.     public HelloVeloctiy(String templateFile) {  
  11.         try {  
  12.   
  13.             Velocity.init();  
  14.   
  15.             /* 
  16.              * Make a context object and populate with the data. This is where 
  17.              * the Velocity engine gets the data to resolve the references (ex. 
  18.              * $list) in the template 
  19.              */  
  20.   
  21.             VelocityContext context = new VelocityContext();  
  22.             context.put("list", getNames());  
  23.   
  24.             /* 
  25.              * get the Template object. This is the parsed version of your 
  26.              * template input file. Note that getTemplate() can throw 
  27.              * ResourceNotFoundException : if it doesn't find the template 
  28.              * ParseErrorException : if there is something wrong with the VTL 
  29.              * Exception : if something else goes wrong (this is generally 
  30.              * indicative of as serious problem...) 
  31.              */  
  32.   
  33.             Template template = null;  
  34.   
  35.             try {  
  36.                 template = Velocity.getTemplate(templateFile);  
  37.             } catch (ResourceNotFoundException rnfe) {  
  38.                 System.out.println("Example : error : cannot find template "  
  39.                         + templateFile);  
  40.             } catch (ParseErrorException pee) {  
  41.                 System.out.println("Example : Syntax error in template "  
  42.                         + templateFile + ":" + pee);  
  43.             }  
  44.   
  45.             /* 
  46.              * Now have the template engine process your template using the data 
  47.              * placed into the context. Think of it as a 'merge' of the template 
  48.              * and the data to produce the output stream. 
  49.              */  
  50.   
  51.             BufferedWriter writer = writer = new BufferedWriter(  
  52.                     new OutputStreamWriter(System.out));  
  53.   
  54.             if (template != null)  
  55.                 template.merge(context, writer);  
  56.   
  57.             /* 
  58.              * flush and cleanup 
  59.              */  
  60.   
  61.             writer.flush();  
  62.             writer.close();  
  63.         } catch (Exception e) {  
  64.             System.out.println(e);  
  65.         }  
  66.     }  
  67.      public ArrayList getNames()  
  68.         {  
  69.             ArrayList list = new ArrayList();  
  70.   
  71.             list.add("HelloVelocity 1");  
  72.             list.add("HelloVelocity 2");  
  73.             list.add("HelloVelocity 3");  
  74.             list.add("HelloVelocity 4");  
  75.   
  76.             return list;  
  77.         }  
  78.      public static void main(String[] args)  
  79.         {  
  80.          HelloVeloctiy t = new HelloVeloctiy("example.vm");  
  81.         }  
  82. }  

4、编译执行,看看输出是不是很简单:

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <list>  
  3.     HelloVelocity 1 is fun!  
  4.     HelloVelocity 2 is fun!  
  5.     HelloVelocity 3 is fun!  
  6.     HelloVelocity 4 is fun!  
  7. </list>  


 5、关于VTL更多的信息,可以查阅相关资料。

原文转自:http://blog.csdn.net/huangyunzeng2008/article/details/7031712

原文地址:https://www.cnblogs.com/jston/p/2916999.html